Bananach
Bananach

Reputation: 2311

Git -- look at older version of specific file without overriding working directory version

Suppose I have worked on the file "foo.c" but I don't think it is ready to commit. Something is not working, so I want to open an older version of "foo.c" where that something was still working. Therefore I do

git checkout HEAD^ foo.c 

But now if I want to go back by using

git checkout HEAD foo.c

the file is back to the time of the HEAD commit, not to just before I checked out the old version.

Upvotes: 1

Views: 61

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521609

When you executed

git checkout HEAD^ foo.c 

You overwrote the changes you made to foo.c in your working directory. So unless you committed or stashed those changes, they may be gone. Next time you do this, make sure you either stash or commit your work to prevent from losing it.

One clean way to deal with this situation would be make a commit of a temporary nature:

git commit -m 'temporary commit'

Then proceed as you were:

git checkout HEAD^ foo.c
# work, test, etc.

When you wish to return back to things the way they were, you simply checkout the HEAD, except this time your changes will be there:

git checkout HEAD foo.c

When you have finished the feature you have in mind, you can git add your files, and amend this temporary commit:

git commit --amend -m 'This is the real commit'

Upvotes: 1

Related Questions