Suppose my history goes that way :
A - B - C - D (master)
If I do git reset B
, I'll got :
A - B (master)
Trouble is, git log
now show me only the history from A to B, and I can't see C and D anymore.
How can I go back to D ?
The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case.
The reset command actually will never delete a commit or change the content of a branch that you do not have currently checked out. It is just simply moving your HEAD pointer around. A very useful tool that is worth mentioning is the reflog.
You should be able to see D with git reflog
.
See this article for instance.
The only time commits are actually deleted is if you git gc --prune (so be careful with that one!).
If you run git reflog right now in a repository you’ve been working in, you’ll see lots of changes that look something like this:
c5c3a82... HEAD@{0}: pull origin featureB: Merge made by recursive.
49d0608... HEAD@{1}: reset --hard HEAD^: updating HEAD
3ed01b1... HEAD@{2}: pull origin featureA: Merge made by recursive.
49d0608... HEAD@{3}: pull origin bugfixJ: Merge made by recursive.
854d44e... HEAD@{4}: commit: Add more cowbell to foo.c
6dbc22d... HEAD@{5}: pull origin bugfixI: Merge made by recursive.
9bdb763... HEAD@{6}: commit: Remove weevils
8518f9d... HEAD@{7}: checkout: moving from wickedfeature to master
These lines can be broken down into 4 parts:
- commit hash,
- commit pointer,
- action,
- and extra info.
If we wanted to get back the commit that was lost at
HEAD@{1}
, we could justgit reset --hard HEAD@{2}
.
Now our current branch (and working copy) are set to the repository state before we did the reset.If we wanted to just see what that state was, we could
git checkout -b temp HEAD@{2}
(orgit checkout HEAD@{2}
if you have git 1.5.0 and up).
Ok, found it.
You can use git reflog
.
I didn't know what it was for, but now I can see it's a log of all the references HEAD have been pointing to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With