Sachin Chaurasiya

Toolbox · Source control

Git Commands

Branching, history inspection, rewriting safely, bisecting and recovering from mistakes.

Last reviewed
2026-09-18
On this page

Everyday

CommandPurpose
git switch -c feature/xNew branch
git fetch --pruneUpdate refs, drop deleted remote branches
git pull --rebaseRebase local commits onto upstream
git add -pStage hunks interactively
git commit --fixup <sha> then git rebase -i --autosquash mainClean fixups
git push --force-with-leaseCaution rewrite the remote branch, refusing if it moved

History and blame

CommandPurpose
git log --oneline --graph --decorate -20Compact graph
git log -S 'secret_key' --onelineCommits that added/removed a string
git log -p -- path/fileChanges to one file
git blame -L 40,60 fileWho changed these lines
git show <sha> --statFiles in a commit
git diff main...feature/xChanges since the branch point

Undo and recover

CommandPurpose
git restore --staged fileUnstage
git restore fileDestructive discard unstaged changes to the file; they cannot be recovered
git revert <sha>New commit that undoes another (safe on shared branches)
git reset --soft HEAD~1Uncommit, keep changes staged
git reflogFind lost commits after a bad reset/rebase
git stash push -m "wip" -u / git stash popShelve including untracked

Debugging

CommandPurpose
git bisect start; git bisect bad; git bisect good v1.4.0Binary search for a regression
git bisect run ./test.shAutomate bisect
git worktree add ../hotfix hotfix/xSecond working tree

Hygiene and security

CommandPurpose
git config --global commit.gpgsign trueSign commits
git verify-commit HEADCheck signature
git filter-repo --path secrets.env --invert-pathsDestructive rewrite all history to drop a file (rotate the secret first)
git gc --prune=nowCaution delete unreachable objects now instead of after the grace period