If your repository ever makes you sweat then git log and git reflog are the cooling towel you did not know you needed. This guide walks through reading the commit graph finding specific changes and even dragging lost commits back from the void. No drama just a little controlled chaos and an emergency plan for your branches.
When you want a compact picture of what happened use the graph view. It shows branch tips merge points and commit messages without forcing you to read a novel.
git log --oneline --graph --decorate --all
This is the go to for a fast mental model of the repo. The graph characters show merges and branches the decorate part shows refs like origin main and your local heads and the oneline format keeps it readable when you are sleep deprived.
If the graph is a forest of noise narrow it down with filters. Useful flags include time author and grep searches. This helps when you are hunting for a fix in a Python file or trying to find who touched that Java test.
git log --since='2 weeks ago'
git log --author='Alice'
git log --grep='bugfix'
git log -S'functionName' -G'pattern'
Use these to zero in on the commit that introduced the behavior you need to revert or inspect. Combining flags is fine and often necessary when you are playing detective.
Reflog records where HEAD and branch tips moved locally. It is not shared with remotes and it is not forever but it is usually enough to rescue a deleted branch or an aborted rebase.
git reflog
Entries look like HEAD@{3} and include a short hash. Read top to bottom for recent moves. If you see the commit you want note the hash or the reflog entry.
Found the hash in reflog Good news. Turn that orphan into a branch or cherry pick it into a current branch.
git checkout -b recover
# or
git cherry-pick # from your current branch
Checkout with a new branch is the safest first move. It gives you a sandbox and avoids rewriting other histories by accident.
Interactive rebase is your tidy tool. Use it to squash reorder or edit old commits. If you must get rid of mistakes locally do the rebase then push carefully.
git rebase -i
# to force push after you have coordinated with your team
git push --force-with-lease origin your-branch
Force pushing is not a crime if you warned people first. If you do it unannounced be prepared to answer angry messages and possibly merge conflicts from teammates.
Once you recover what you need tidy up. You can expire reflog entries and run garbage collection if you are maintaining long lived repos but be careful. Expiring reflogs too aggressively removes your safety net.
git reflog expire --expire=90.days --all
git gc --prune=now
These commands help maintain repository size but only run them when you understand the consequences and you are allowed to touch repo history.
That is it. You now know how to inspect history recover lost commits and tidy the graph like a mildly competent repo surgeon. Keep reflog settings sensible avoid reckless garbage collection and when in doubt make a branch first and panic later.
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.