10 Advanced Git Commands for Faster Workflows
Almost every modern developer uses Git daily, but most stick to the basic workflow: git pull, git add, git commit, and git push. While this covers the absolute basics, Git is incredibly powerful and offers advanced commands that can rescue you from mistakes, streamline your commits, and save you hours of manual work.
In this article, we cover 10 advanced Git commands that will immediately supercharge your development velocity.
1. Quick Undo with git commit --amend
Made a typo in your last commit message or forgot to stage a file? Don't create a new "fix typo" commit. Instead, use:
git commit --amend -m "Your updated commit message"
This merges your staged changes directly into the previous commit.
2. The Time-Traveler: git reflog
Ever ran git reset --hard or accidentally deleted a branch and felt your heart sink? Fear not. Git records every single action you take in your local repository.
git reflog
This lists the exact history of your HEAD pointer. Find the state you want, and reset back to it using git reset --hard <commit-hash>.
3. Stash Specific Changes with git stash -p
Sometimes you have several files modified, but you only want to temporarily store (stash) some of them. Stash interactively using:
git stash -p
Git will prompt you patch-by-patch, allowing you to decide exactly what gets stashed.
4. Clean Up Commit History with git rebase -i
Before submitting a Pull Request, clean up your messy commits (like "WIP" or "debug"). Use interactive rebase for the last N commits:
git rebase -i HEAD~4
This opens a text editor where you can pick, squash (combine), reword, or drop commits.
5. Cherry-Pick Commits across Branches
Need a specific bug fix commit from a development branch in your release branch without merging the entire branch? Use cherry-pick:
git cherry-pick <commit-hash>
6. Find the Buggy Commit with git bisect
When a bug appears out of nowhere and you have hundreds of commits, finding the exact change that broke the code is tedious. git bisect uses a binary search algorithm to pinpoint the exact commit:
git bisect start
git bisect bad # Mark current commit as bad
git bisect good v1.0 # Mark a known working tag/commit as good
Git will checkout commits halfway, and you mark them good or bad until it identifies the culprit.
7. Search Code History with git log -S
Need to find when a specific function name or string was introduced or removed?
git log -S "functionName"
8. Clean Untracked Files Safely
To clean up your directory from generated files and untracked junk, use git clean. Run a dry run first to see what will be deleted:
git clean -fdn
And then delete them permanently using:
git clean -fd
9. Show Branch Merges Visualized
A clean command-line graph of your commit history:
git log --graph --oneline --all --decorate
10. Automatically Prune Stale Branches
Clean up local references to remote branches that have already been deleted:
git fetch --prune
Summary
Mastering these Git commands transitions you from a basic git user to a command-line power user. Share these with your team to help everyone keep their commit histories clean and git anxiety at bay!