Toolbox · Source control
Git Commands
Branching, history inspection, rewriting safely, bisecting and recovering from mistakes.
On this page
Everyday
| Command | Purpose |
|---|---|
git switch -c feature/x | New branch |
git fetch --prune | Update refs, drop deleted remote branches |
git pull --rebase | Rebase local commits onto upstream |
git add -p | Stage hunks interactively |
git commit --fixup <sha> then git rebase -i --autosquash main | Clean fixups |
git push --force-with-lease | Caution rewrite the remote branch, refusing if it moved |
History and blame
| Command | Purpose |
|---|---|
git log --oneline --graph --decorate -20 | Compact graph |
git log -S 'secret_key' --oneline | Commits that added/removed a string |
git log -p -- path/file | Changes to one file |
git blame -L 40,60 file | Who changed these lines |
git show <sha> --stat | Files in a commit |
git diff main...feature/x | Changes since the branch point |
Undo and recover
| Command | Purpose |
|---|---|
git restore --staged file | Unstage |
git restore file | Destructive 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~1 | Uncommit, keep changes staged |
git reflog | Find lost commits after a bad reset/rebase |
git stash push -m "wip" -u / git stash pop | Shelve including untracked |
Debugging
| Command | Purpose |
|---|---|
git bisect start; git bisect bad; git bisect good v1.4.0 | Binary search for a regression |
git bisect run ./test.sh | Automate bisect |
git worktree add ../hotfix hotfix/x | Second working tree |
Hygiene and security
| Command | Purpose |
|---|---|
git config --global commit.gpgsign true | Sign commits |
git verify-commit HEAD | Check signature |
git filter-repo --path secrets.env --invert-paths | Destructive rewrite all history to drop a file (rotate the secret first) |
git gc --prune=now | Caution delete unreachable objects now instead of after the grace period |