Git Reflog: Your Local Time Machine
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
- time machine which you admire become not really helpful. But you should know that "
" 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
onto the latest commit of the master. The commit before the rebase is
Let try to reset the local to that commit. The current branch is
The local was reseted to this state - undo the rebase. 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
. Now let try to redo the rebase.
Redo almost everything
Redo also can be done by
Do not apply these thing on the public branch - your nerd co-workers will hate you a lot.
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.
Subscribe to get your daily round-up of top tech stories!