This chapter follows Merging versus rebasing.
Most days you commit, push, and merge. These tools matter when history is large, when a regression landed weeks ago, or when automation must enforce checks before commits leave your machine.
Revision syntax
HEAD is the current commit. HEAD^ or HEAD~1 is the first parent; HEAD~5 walks five first-parent steps. Merge commits have multiple parents—HEAD^2 is the second parent:
git show HEADgit show HEAD^git show HEAD~5git show HEAD~5^2git show HEAD@{"1 month ago"}@{...} reflog-based dates resolve “where was this branch last month?”
Log and search
git log --onelinegit log feature --onelinegit log --graph --decorate --onelinegit log --grep "fix menu" --onelinegit log -G"function login" --onelinegit log -G"login" -pgit log -5 --onelinegit log HEAD~5..HEAD --onelinegit log feature..main --onelinegit log feature...main --oneline--grep matches commit messages. -G matches diffs (content changes). A..B shows commits reachable from B but not A. A...B shows symmetric difference (either side since merge-base)—useful for “what changed on both branches since they diverged.”
Blame
git blame src/app.tsgit blame -L 40,60 src/app.tsShows author and commit per line—helpful for understanding why a line exists, not for assigning blame in a team culture sense.
Reflog
Git logs where HEAD and branch tips were, even after reset or rebase:
git refloggit reflog show feature/loginRecover a “lost” commit by creating a branch at an old reflog entry:
git branch recovered abc1234Reflog entries expire (default ~90 days); unpushed work still needs remotes as backup.
Filter history with git filter-repo
Remove secrets or huge files from all commits:
pip install git-filter-repogit filter-repo --path secrets.env --invert-pathsRequires force-push and team coordination. Prefer git filter-repo over legacy filter-branch.
Bisect
Find which commit introduced a bug with binary search:
git bisect startgit bisect badgit bisect good v1.0.0# test each checkout; mark good or badgit bisect good # or git bisect badgit bisect resetAutomate with git bisect run ./test.sh.
Worktrees
Check out multiple branches in separate directories from one repository:
git worktree add ../project-hotfix hotfix/123git worktree listgit worktree remove ../project-hotfixAvoids stashing when you need parallel checkouts without a second clone.
Hooks
Scripts in .git/hooks/ (or shared via core.hooksPath) run on events:
| Hook | Typical use |
|---|---|
pre-commit |
Lint staged files |
commit-msg |
Enforce message format |
pre-push |
Run tests before push |
Sample pre-commit installs via tools like husky on Node projects. Hooks are local unless the team ships a setup script— they are not cloned from GitHub automatically.
Rerere
Reuse recorded resolution of merge conflicts:
git config rerere.enabled trueWhen you resolve the same conflict again (e.g. after rebase), Git can reapply your prior resolution. Valuable on long-lived branches that merge main often.