DEV Community

Cover image for Git Reflog
Suprabha
Suprabha

Posted on

Git Reflog

By the name git reflog - Manages reflog information. It keeps tracks of everything which you do locally.

Letโ€™s understand the git reflog and git log ๐Ÿ‘€

git reflog keep tracks of everything you have done locally.

example:

  • keep track of commit history,
  • if you have done hard reset,
  • it keeps track of git commit --amend as well

For git log, it never track the above commits(like reset and --amend).

The git reflog shows all your trials and errors. The git log just shows a clean and polished version of your work history.

So letโ€™s say you have amend the commit, hence there will be new commit. If you do a reset and skip back a few commits in your history, those commits you skipped over won't show up in the log.

There is more to talk, letโ€™s discuss it ๐Ÿ˜…

While working on repo, and habits of pushing code into main branch ๐Ÿ˜…

I started working on one feature without taking pull from main branch, after making changes when I tried to push changes to the main branch. I got following errors:

$ git push origin main
// I got to know I had to take pull from main (which some how I missed)
// and without taking pull I pushed forcefully
Enter fullscreen mode Exit fullscreen mode

And then I did the unthinkable ๐Ÿ˜‚

$ git push origin main -f
// successfully pushed to main branch
Enter fullscreen mode Exit fullscreen mode

Later one of my colleague took pull from the main branch, and she was ๐Ÿ˜ณ๐Ÿ˜ณ๐Ÿคฏ๐Ÿคฏ.

Then I got to know that by force pushing my changes, I have removed her commit form the remote branch and the icing on the cake was that she has pulled the changes and now her work vanished from her local machine too! ๐Ÿ™ˆ

We were not able to see those commits using git log(almost 5-6 commit IDโ€™s were missing) ๐Ÿ˜ฅ

To revert back my commit, I had to find the last commit ID which has been removed from commit history. If I would have not closed my terminal tab, then there was a chance to get the last commit ID.

How would not closing terminal tab have been helpful ๐Ÿค”

So, while doing forcefully push, you get the message:

+ aauuii1...789011 HEAD -> branchname (forced update)
Enter fullscreen mode Exit fullscreen mode

Here, aauuii1 is previous HEAD, and 789011 is new which just pushed forcefully.
So you can easily revert to that commit ID ๐Ÿ™Œ๐Ÿป

But seems, I am not that lucky, as I already closed my terminal tabs ๐Ÿคฆโ€โ™€๏ธย 

After ๐Ÿ” , I got to know the context about git log and git reflog.

git reflogย tells you the chronological history of what you did.

Their I found this ๐Ÿ‘‡ย command,

$ git reflog show remotes/origin/branchname
Enter fullscreen mode Exit fullscreen mode

It shows the forced update (789011) and previous commit (aauuii1).

After getting the last commit ID, I created new branch and reset to the commit ID

$ git checkout -b branch_name
$ git reset --soft aauuii1

// yayy! Its done ๐Ÿ˜
Enter fullscreen mode Exit fullscreen mode

git reflog allows you to go back and find those commits as it'll give you the commit ids. It is really a life saver ๐Ÿฅ

Moral of the story ๐Ÿฅ‡

Never give up, with little patience and some googling skill you might end up saving hours of work ๐Ÿ’›

Buy Me A Coffee

๐ŸŒŸ Twitter ๐Ÿ“š Ebooks ๐ŸŒŸ Instagram

Top comments (0)