Git reflog can help you restore lost work

Using git reflog is a powerful tool that you should add to your developer toolkit.

~

A lot of the software developers I’ve interacted with hold a basic working knowledge of git which I’ve always found surprising as a lot of pieces sound a lot scarier to hear than they are to use. An example of this would be git rebasing - people immediately seem to worry about this even though at the end of the day its a relatively simple concept - you have a stack of commits and you want to trim them down.

Another one is the git reflog - there are times in your career when you may need to use the reflog to rescue yourself.

In an existing profile, type git reflog and you’ll be presented with a view similar to this:

git reflog

If you want to make it a little easier to work out when each change was made, then tag on --date=iso and run that instead:

git reflog --date=iso

and you’ll get the same output as above but with time and dates:

git reflog with date/time

What this shows you is all the activity that has taken place in the repository. It’ll show you merges, rebases, pulls, commits and more. It will also contain all changes in your repo that were not committed/staged as well - meaning you can recover accidental deletions and rectify other mistakes. For each of the HEAD@{N} lines, you can check each of those lines out, by using:

git checkout HEAD@{9}

If you want to really see everything that has happened to your repository then you can actually pass an extra flag to git reflog like this:

git reflog show --all

You can find more information and some extra reading at the following sites:

I’ve hope you found this post informative enough to be confident in exploring everything git can offer you.


~

Comments