DEV Community

Renaud Humbert-Labeaumaz
Renaud Humbert-Labeaumaz

Posted on • Originally published at rnowif.github.io on

Keep Calm and Use Git Reflog

Git is a very powerful tool but it takes a fair amount of time to master it. It can be particularly easy to break or lose things when you amend some commits, resolve conflicts or force push your changes.

In this blog post, I will show you how to use the git reflog command to retrieve commits that you thought you lost forever.

Retrieve an amended commit

The first use case I will show is when you amend a commit:

$ git init
$ echo "Hello" > hello.txt
$ git add hello.txt && git commit -m "Add hello"
$ echo "Hello!" > hello.txt
$ git add hello.txt && git commit --amend
Enter fullscreen mode Exit fullscreen mode

If you think that the first commit is lost, read on.

The git reflog command allows you to display all git commands that you performed on your repo and, most importantly, the associated commit hash. In fact, these commits are not lost, they are just not in the tree anymore but you can still check them out, cherry-pick them or do whatever you need to do.

$ git reflog
3e6e190 HEAD@{0}: commit (amend): Add hello
51587bc HEAD@{1}: commit (initial): Add hello
$ git diff 51587bc 3e6e190
diff --git a/hello.txt b/hello.txt
index e965047..10ddd6d 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-Hello
+Hello!
Enter fullscreen mode Exit fullscreen mode

Recover an erroneous merge

The second scenario is when you rebase a branch on another one and you have conflicts.

$ git checkout -b develop
$ git checkout master
$ echo "Hello, world!" > hello.txt
$ git add hello.txt && git commit -m "Hello, world!"
$ git checkout develop
$ echo "Hello, you!" > hello.txt
$ git add hello.txt && git commit -m "Hello, you!"
$ echo "World!" > world.txt
$ git add world.txt && git commit -m "Add world.txt"
Enter fullscreen mode Exit fullscreen mode

This is the resulting tree (note that the hello.txt has been modified on the two branches and there is a conflict):

* a5c7ca5 - (HEAD -> develop) Hello, you!
| * 4d016c5 - (master) Hello, world!
|/  
* 3e6e190 - Add hello
Enter fullscreen mode Exit fullscreen mode

Unsurprisingly, the reflog looks like this:

$ git reflog
21c2ce2 HEAD@{0}: commit: Add world.txt
a5c7ca5 HEAD@{1}: commit: Hello, you!
3e6e190 HEAD@{2}: checkout: moving from master to develop
4d016c5 HEAD@{3}: commit: Hello, world!
3e6e190 HEAD@{4}: checkout: moving from develop to master
3e6e190 HEAD@{5}: checkout: moving from master to develop
3e6e190 HEAD@{6}: commit (amend): Add hello
51587bc HEAD@{7}: commit (initial): Add hello
Enter fullscreen mode Exit fullscreen mode

Now we rebase develop on master and we resolve the conflicts.

$ git rebase master
// ... resolve merge conflicts
Enter fullscreen mode Exit fullscreen mode

The thing is that we made a mistake and took the wrong version of hello.txt. Since we did a rebase, we rewrote history and we could think we lost the β€œgood” version of hello.txt.

In fact, the reflog will display a line for each commit you rebased so it is very easy to pick the one where you made a mistake.

$ git reflog
ae9d1d8 HEAD@{0}: rebase finished: returning to refs/heads/develop
ae9d1d8 HEAD@{1}: rebase: Add world.txt
4d016c5 HEAD@{2}: rebase: checkout master
21c2ce2 HEAD@{3}: commit: Add world.txt
a5c7ca5 HEAD@{4}: commit: Hello, you!
$ git diff 21c2ce2
diff --git c/hello.txt w/hello.txt
index 624d875..af5626b 100644
--- c/hello.txt
+++ w/hello.txt
@@ -1 +1 @@
-Hello, you!
+Hello, world!
Enter fullscreen mode Exit fullscreen mode

If you want to go back and fix it, you just have to check the commit out, delete the current develop branch (since it is not in the correct state) and create a new develop branch based upon the right commit.

$ git checkout 21c2ce2
$ git branch -D develop
$ git checkout -b develop
Enter fullscreen mode Exit fullscreen mode

Conclusion

I think you got the idea, Git keeps everything that you committed. Thus, don’t panic if you find yourself in a situation where you erase a previous commit and think about the git reflog command to dive into your history and look for the information you thought you lost!

Top comments (0)