20 Advanced Git Command-Line Tricks Every Developer Should Know
Supercharge your Git workflow with these lesser-known yet powerful commands and shortcuts.

Unlock the Hidden Powers of Git
20 Advanced Git Command-Line Tricks Every Developer Should Know
Git is the lifeline of every developer’s version control workflow — yet most of us only scratch the surface.
You probably use git add
, git commit
, and git push
daily. Maybe even git stash
or git rebase
if you're a bit fancy.
But there’s a treasure trove of Git commands that can make your life significantly easier — and faster.
Whether you’re trying to debug a tricky issue, clean up messy history, or simply boost your efficiency, these 20 advanced Git tricks are your secret weapons.

1. Quickly Fix the Last Commit
Ever forgot to add a file or mistyped a commit message?
git commit --amend
This lets you modify the last commit — be it to change the message or include more changes — without creating a new one.
2. See Who Changed What (and When)
Want to know who last edited a specific line of code?
git blame <file>
It shows line-by-line commit info, which is invaluable when tracking down bugs or regressions.
3. Search Commit History for Keywords
You can search the entire Git history for a specific word:
git log -S'keyword'
This finds commits that introduced or removed the keyword. Great for hunting down when something changed.
4. Delete Local Branches That Are Already Merged
Clean up your local branches:
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
This deletes branches that have already been merged into your current branch.
5. View a Beautiful Git Log Graph
See a visual history of your project with:
git log --oneline --graph --all --decorate
A surprisingly helpful way to understand your repo’s structure.
6. See What’s in the Stash
Before applying a stash, you can inspect what’s inside:
git stash show -p
Add -p
for a patch-style diff of the stashed changes.
7. Reuse Code from Another Branch Without Switching
Want to cherry-pick a specific file from another branch?
git checkout other-branch -- path/to/file
This pulls the file from other-branch
into your current one — without a full branch switch.
8. Stage Portions of Files
Only want to commit part of a file?
git add -p
You’ll be prompted to review and selectively stage “hunks” of changes. Useful for clean commits.
9. Find Out What’s Taking Up Space
To inspect where your .git
folder bloat is coming from:
git rev-list --objects --all | sort -k 2 | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sort -k 3 -n -r | head -20
This gives you the top 20 largest objects in your Git history.
10. Temporarily Work Without Committing
Need to quickly test something but don’t want to commit?
git stash -k
This stashes your uncommitted changes but keeps your staged ones. Perfect for short experiments.
11. Show All Ignored Files
Want to see what’s being ignored by .gitignore
?
git status --ignored
Helpful when debugging ignored files that are still showing up in commits.
12. Abort a Merge (Cleanly)
If a merge goes south:
git merge --abort
This reverts everything back to the state before the merge began.
13. View File History (Across Renames)
Get complete history of a file, even if it was renamed:
git log --follow <file>
Unlike the regular git log
, this respects file renames.
14. Grep Through Your Git History
Want to find commits that contain a certain string?
Combines commit message searching with code diff for high precision.
Combines commit message searching with code diff for high precision.
15. Undo a Pushed Commit (Carefully)
If you need to revert a commit you already pushed:
git revert <commit_hash>
This creates a new commit that undoes the previous one — the safe way to “undo” on shared branches.
16. Rebase But Keep Merges Intact
Standard rebasing flattens history, but you can preserve merge commits:
git rebase --preserve-merges <branch>
Great for reorganizing without losing context.
17. View Remote Branches You’ve Never Checked Out
See what’s out there on the remote:
git branch -r
You can then do:
git checkout origin/feature-cool-stuff
Or:
git checkout -b feature-cool-stuff origin/feature-cool-stuff
18. Auto-Delete Merged Branches on Pull
Enable this once and forget about cleanup:
git config --global fetch.prune true
Now git pull
will automatically remove deleted remote branches locally.
19. Find Unreachable Commits (Lost Work)
Run a Git “garbage collection” dry-run:
git fsck --lost-found
This can sometimes rescue unreferenced commits, especially after rebases or resets gone wrong.
20. Bisect to Find the Bug
Automate the hunt for a commit that introduced a bug:
git bisect start
git bisect bad
git bisect good <commit_hash>
Git will walk you through checking out and testing commits until it narrows down the culprit.
Bonus Tip: Make Your Own Aliases
Tired of typing long commands? Customize Git with aliases in your config:
git config --global alias.lg "log --oneline --graph --all --decorate"
Now just use:
git lg
Wrapping Up
Git is more than just a tool for saving your work — it’s a power tool for managing codebases, collaborating seamlessly, and recovering from mistakes with ease.
Mastering these lesser-known commands will not only make you faster but also more confident in your workflow. Don’t memorize them all. Instead, bookmark this article and come back when you need a boost.
Want to really level up? Pick three of these tricks, apply them to your current project, and see how much smoother your Git experience becomes.
Every Git ninja starts as a Git apprentice — but with the right tricks, mastery is just a few commands away.
