Extract a file from an old commit to a path outside my working directory

I have a working directory at ~/gitrepo/ (this is where .git folder is, and all my other files as usual).

I have an old commit whose SHA-1 hash I know. In it there was a file foo.cpp that I now want to bring back to another directory (just to not mess up my working tree), for instance here:

  • Merge commit from Master-branch to another branch, but not to merge two branches
  • Splitting a subdirectory with submodules into a separate git repository
  • Git (1.7.10) asks me every time for username and password
  • Is there a way to add a link to the current repo in Github README.md?
  • How do I reduce the size of a bloated Git repo by non-interactively squashing all commits except for the most recent ones?
  • Best git like versioning tool for many small files
  • ~/Desktop/foo.cpp

    How to do this?

  • Should I use Git branches to store unrelated code?
  • “no such table” error on Heroku after django syncdb passed
  • How to send password in command using ssh command
  • How to remove/hide GitExtensions toolbar from Visual Studio?
  • Git hook: add a new file to repo if a new branch is created
  • Git pull - error entry notuptodate cannot merge
  • 3 Solutions collect form web for “Extract a file from an old commit to a path outside my working directory”

    git checkout SHA -- foo.cpp
    cp foo.cpp ~/Descktop/foo.cpp

    After that you’ll have edited foo.cpp , so you may wanna revert it back with git reset or something.

    You can find your commit on github website, and download the file you want from there.

    This is what I did to get an old copy of an Informix 4GL file. First, I copied re_trnfunc.4gl to re_trnfunc.4gl.sav, because I had never tried this before.

    Then, I entered:

    git log --shortstat --pretty

    I looked for the commitment that corresponded to the comment that told me which version I wanted.

    commit 6fbe9303e7f80150fdf6abdc8f926a802f8a171e
    Author: Charles M. Norton <someone@somewhereville.com>
    Date:   Wed Jul 1 14:13:58 2015 -0400
        Put in new re payment distro order, interest, charges, and principal.
     1 files changed, 34 insertions(+), 30 deletions(-)

    Then I entered (as shown in @ДМИТРИЙ МАЛИКОВ ‘s answer)

    git checkout 6fbe9303e7f80150fdf6abdc8f926a802f8a171e -- re_trnfunc.4gl

    I examined the file, saved it elsewhere, and then needed to put re_trnfunc.4gl the way it was before the checkout.

    I entered:

    git reset HEAD re_trnfunc.4gl

    I could have eliminated having to reset by checking out the file to a temp name after the -- in the

    git checkout 6fbe9303e7f80150fdf6abdc8f926a802f8a171e -- temp_re_trnfunc.4gl


    Git Baby is a git and github fan, let's start git clone.