Skip to content

Git — the bits I forget

Not a tutorial. Personal reference for the operations that I do rarely enough to forget.

Undoing things

# Discard unstaged changes to a file
git restore <file>

# Discard staged changes (unstage but keep working tree)
git restore --staged <file>

# Reset to a specific commit, discarding everything after
git reset --hard <sha>

# Undo the last commit, keep the changes staged
git reset --soft HEAD~1

Stash gymnastics

# Stash including untracked files
git stash push -u -m "wip on the thing"

# List stashes with diffs
git stash list -p

# Apply a specific stash by index
git stash apply stash@{2}

Bisect

git bisect start
git bisect bad                  # current is broken
git bisect good v1.2.0          # this tag worked
# git checks out the midpoint
# test, then:
git bisect good   # or bad
# repeat
git bisect reset

Rebasing and history rewriting

# Interactive rebase, last 5 commits
git rebase -i HEAD~5

# Force push safely (refuses if remote moved)
git push --force-with-lease

Never --force to a shared branch.

Worktrees — the underused feature

git worktree add ../feature-branch feature-branch

Multiple checked-out branches sharing one repo. Faster than cloning twice.