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:
How to do this?
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 <email@example.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.
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