If your commit history looks like a crime scene and you are missing a few suspects do not panic yet. Git keeps a trail of where HEAD and branches have been and with a few commands you can read the story or even resurrect a missing commit. This guide helps you inspect history with git log and find recent moves with reflog so you can recover work without becoming the office cautionary tale.
Start with the obvious. Run the plain command to list commits with author date and message. It gives a readable timeline for most needs.
git log
If you want a slightly friendlier view add options to limit output or show diffs for a commit. But the default is where you begin looking.
When the commit graph looks like someone tried to knit spaghetti use a compact visual view. The following shows each commit on one line with branch names and a nice ASCII graph which works in terminals and in automation logs.
git log --oneline --graph --decorate --all
This helps you spot the current HEAD branch and merges fast. It also plays nicely when you are on GitHub or GitLab and want to match UI history to what is on your machine.
The reflog is the safety net that remembers where HEAD and refs pointed recently even after resets or forced pushes. It is not a permanent record but it is incredibly useful for short term rescue missions.
git reflog
Scan the list for the commit hash you want to recover. Each line shows an action such as checkout reset or commit along with the hash and a human friendly note.
Once you have the hash you can either make a branch from it or move a branch pointer. Making a branch is the safest and least dramatic option.
git checkout -b rescue abc1234
That creates a branch named rescue at commit abc1234 so the commit is easy to work with and can be pushed to remote for safe keeping. If you prefer the newer command use
git switch -c rescue abc1234
If you need to rewind the current branch and you are certain of your choice you can reset hard. Do not do this without a backup branch unless you enjoy adrenaline.
git reset --hard abc1234
Git eventually prunes unreachable objects during garbage collection. That means reflog and dangling commits may disappear after some time. Keep important recovery points on a branch or a tag so the reflog is a helper not the only lifeline.
If you follow these steps you will spend less time apologizing in team chat and more time writing code. Or at least less time rewriting history by accident. Keep branches and tags for the commit you care about and treat reflog as a fast rescue tool not a permanent insurance policy.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.