Advanced Git Commands Every Developer Should Know
Once you’ve mastered git add, commit, push, and pull, these advanced commands will save you hours.
Interactive Rebase
Clean up commit history before pushing:
git rebase -i HEAD~5Opens an editor where you can:
pick— keep the commitreword— change the messagesquash— merge into the previous commitfixup— merge and discard the messagedrop— remove the commit
Use case: Combine “fix typo” and “oops forgot file” commits into one clean commit before opening a PR.
Git Reflog
Git’s safety net. Records every movement of HEAD:
git reflogUse case: You accidentally reset to the wrong commit. Find the lost commit in reflog and git reset --hard <sha> to restore it.
Git Bisect
Binary search to find the commit that introduced a bug:
git bisect start
git bisect bad # current commit is broken
git bisect good v2.0.0 # this tag was working
# Git checks out a middle commit. Test it.
git bisect good # if this commit works
# or
git bisect bad # if this commit is broken
# Repeat until Git identifies the first bad commit
git bisect resetUse case: A test started failing sometime in the last 50 commits. Bisect finds the exact commit in ~6 steps.
Git Cherry-Pick
Apply specific commits from another branch:
git cherry-pick <commit-sha>
git cherry-pick <sha1> <sha2> <sha3> # multiple commits
git cherry-pick main..feature # range of commitsUse case: A bug fix was committed to the develop branch but needs to be in main immediately without merging the whole branch.
Git Stash Advanced
git stash push -m "WIP: refactoring auth" # named stash
git stash list # view all stashes
git stash show -p stash@{1} # view changes
git stash branch new-feature stash@{1} # create branch from stashUse case: You’re working on a feature but need to fix an urgent bug. Stash your work, fix the bug, then git stash pop to resume.
Git Submodules
Include one repo inside another:
git submodule add https://github.com/example/lib.git lib/
git clone --recurse-submodules <repo-url>
git submodule update --remote # update all submodulesUse case: You have a shared library used by multiple projects. Keep it as a submodule to version it independently.
Git Worktree
Work on multiple branches simultaneously:
git worktree add ../project-release release-branch
git worktree add -b new-feature ../project-feature
git worktree list
git worktree remove ../project-releaseUse case: You need to make a hotfix on main without stashing or committing your current feature branch work. Create a worktree for main.
Git Log Formatting
# One-line graph with branches
git log --oneline --graph --all --decorate
# Show changes in each commit
git log -p
# Search commits by message
git log --grep="fix bug"
# Search commits by author
git log --author="Alice"
# Custom format
git log --pretty=format:"%h %s by %an, %ar"Related: Learn how to undo commits and Git branching strategies.