brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find lost commits with git reflog

Git
~/tips/tip-git-reflog-recover-commits

git reflog

Lists where your branch tips have been so you can bring back commits after a reset.

After a bad git reset --hard it looks like your commits are gone. Most of the time they are not.

Git keeps a log of where your branch tips and HEAD have been. It is called the reflog. The docs describe it as a record of when the tips of branches and other references were updated in your local repository. Every commit, reset, rebase and branch switch leaves a line in it.

git reflog
git branch rescue HEAD@{2}

The first command lists the moves with the newest on top. HEAD@{2} means where HEAD was two moves ago. Find the line from before things went wrong and put a new branch on it. Now the commits have a name again.

I prefer git branch over a second git reset --hard for the rescue, because the branch does not touch your working tree. You can look around first and decide later.

There are two caveats. The reflog is local, so it does not travel with a clone and your colleague has no idea what is in yours. And it expires. By default, entries that are still reachable are kept for 90 days and unreachable ones for 30 days. Also, it only knows about commits. Changes you never committed are not in there.

Further reading: git reflog documentation, On this site: git commit –fixup plus –autosquash keeps your history clean without any editing, On this site: tell git blame to ignore a big reformatting commit, On this site: Guide: let git bisect find the commit that broke your code

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link