On this page
Git Operations Manual
History and Search
delta is a modern alternative to git diff: it provides a sidebar, line numbers, and syntax highlighting. Configuration: git config --global pager.diff delta && git config --global pager.log delta
# Commit history
# Who changed what
# Binary search for the commit that introduced a bug
# → Git checks out an intermediate version → test → git bisect good/bad → ... → locate the first bad commit
# Recover lost commits
# → Find the hash of the reset/dropped commit → git checkout -b recovery <hash>
Branches and Merges
# merge: preserves branch history, creates a merge commit
# Use case: Merging a feature branch into main, preserving the information that "this feature was developed separately"
# rebase: moves the current branch's commits to the end of the target branch, creating a linear history
# Use case: Aligning a feature branch with main before submitting a PR, keeping a linear history
# When rebase conflicts occur:
# cherry-pick: pick a single commit from another branch
# Use case: Merging only a specific bug fix into a release branch, without bringing in other changes
# Delete branches that have been merged
| |
Stashing and Undoing
# Stash current changes (before switching branches)
# Undoing
# Undoing a commit that has already been pushed (remote):
# vs reset: revert is suitable for public branches (does not break others' history), reset is suitable for local branches
Remote
# Safe force push: only push if the remote hasn't been changed by others
# Safer than --force: if a colleague pushed after your last fetch, this will reject the push, preventing overwrites
Submodule
Worktree
# Check out different branches in different directories within the same repo (no need to clone twice)
# → The ../hotfix directory is the working tree for hotfix-branch, without affecting the current directory
# Use case: Fixing a bug in another branch while running long tests
Tag