Greg's blog

Git superpowers

At my old work I often was the one who was approached after someone ran into some issue with git. Git can be a pain sometimes, but I know one superpower that will get you out of any sticky situation caused by a bad merge, rebase mistake or "oops I didn't mean to reset --hard". It's git reflog! Reflog shows you the reference logs of where branches and references were updated in the local repository. For example, if you checkout a branch then run git reflog you might see something like:

b481299 HEAD@{0}: checkout: moving from <branch_a> to <branch_b>

This can be really useful because you can see the changes to the HEAD tag over time. So if you've done a bad merge and want to reset history, you can! Just git reset --hard "HEAD@{5}" (changing the number as you need). Note the quotes are required when running the command on Powershell.

Bonus: The best git superpower is having an understanding of the directed acyclic graph, or DAG, which is the data structure underlying git. An excellent video to learn this is Git for Ages 4 and Up.