Git Reflog: Your Local Time Machine

Written by esca | Published 2020/05/03
Tech Story Tags: reflog | git | undo | redo | version-control | code-quality | gitflow | beginners

TLDR There is another kind of git history which been keep tracked in your local and your local only, which is so-call reflog. This reflog stores the whole shebang of you local activities and you can undo almost everything with it. The reflog only available on you local and it could be pruned(trimmed) automatically. It won't last forever, and reflog won't be available forever. Do not apply these things on the public branch - your nerd co-workers will hate you.via the TL;DR App

Sometimes, we make do some unintentionally merge or rebase which we wish we could recover to the previous stage. But there is no commit for you to get back. That moment, you probably feel like the
git log
- time machine which you admire become not really helpful. But you should know that "
git log
" is not the only way to trace the history. There is another kind of git history which been keep tracked in your local and your local only, which is so-call reflog. This reflog stores the whole shebang of you local activities and you can undo almost everything with it.

Reset you local to almost any moment

We can use the git reset <hash> to restore to any moment listed in the reflog. This is my local after I rebase branch
feature/my-new-branch
onto the latest commit of the master. The commit before the rebase is
bc27e0b
Let try to reset the local to that commit. The current branch is
feature/my-new-branch
.
The local was reseted to this state - undo the rebase. Branch
feature/my-new-branch
base commit was shifted back to the original.
I hope this could surprise you a little when this approach can even help to undo the rebase - which is the type of change not logged in
git log
. Now let try to redo the rebase.

Redo almost everything

Redo also can be done by
git reset

Warning

Do not apply these thing on the public branch - your nerd co-workers will hate you a lot.
Why not?
All the nerds sharing the same branch will have to fix the issues if you push these changes to the origin branch. A lot of problems such as base commit of the sub-branch they branched from the shared branch also be affected. And a lot more which I can not predict.

Git reflog won't last forever

The reflog only available on you local and it could be pruned(trimmed) automatically.

Published by HackerNoon on 2020/05/03