Git and GitHub · Part 2 · Branches and merging

Part 2 summary: Branches and merging

The big idea

A branch is just a name pointing to a commit, so making one is instant. Work happens on separate branches, and merging or rebasing brings it back together. When two branches change the same lines, Git asks a person to decide.

Each lesson in one line

  1. What a branch really is: a named pointer to one commit, stored in a 41-byte file. HEAD points to the current branch.
  2. Branch commands: git switch -c creates and switches, -m renames, -d deletes.
  3. Merging: a fast-forward slides the branch forward; a three-way merge makes a merge commit with two parents.
  4. Merge conflicts: edit the file, delete the markers, git add, then commit.
  5. Rebasing: replays a branch on top of another for a straight history, with new hashes.
  6. Interactive rebase: reword, squash, fixup, drop, reorder or edit commits before sharing them.
  7. Stash and cherry-pick: stash puts unfinished work aside; cherry-pick copies one commit to another branch.

Key terms

TermMeaning
BranchA name that points to one commit and moves forward with each new commit
HEADA pointer to the current branch
Detached HEADHEAD pointing straight at a commit instead of a branch
Common ancestorThe last commit two branches share
MergeBringing another branch's work into the current branch
Fast-forwardA merge that just slides the branch forward, with no new commit
Three-way mergeA merge that combines both sides from their common ancestor
Merge commitA commit with two parents, made by a three-way merge
Merge conflictBoth branches changed the same lines, so a person has to decide
Conflict markersThe <<<<<<<, ======= and >>>>>>> lines Git writes around a conflict
Ours / theirsThe current branch's version / the incoming one (swapped during a rebase)
RebaseReplaying a branch's commits on top of another branch
Interactive rebaseEditing a list of recent commits to clean up the history
Squash / fixupCombining a commit into the one before it, with or without its message
ORIG_HEADWhere the branch was before a merge or rebase, for undoing it
StashA drawer for unfinished changes
Cherry-pickCopying one commit onto the current branch, with a new hash

Essential commands

CommandWhat it does
git branchLists branches
git switch -c nameCreates a branch and switches to it
git switch nameSwitches to a branch
git switch -Goes back to the previous branch
git branch -m newRenames the current branch
git branch -d nameDeletes a merged branch
git merge branchMerges a branch into the current one
git merge --abortCancels a merge in progress
git rebase mainReplays the current branch on top of main
git rebase --continueGoes on after fixing a conflict
git rebase -i HEAD~NCleans up the last N commits
git stash -u -m "…"Puts all changes aside, with a label
git stash popBrings the newest stash back
git cherry-pick hashCopies one commit here
git log --oneline --graph --allDraws every branch

Rules to remember

  • A merge goes into the current branch. Switch to the branch that receives the work first.
  • Keep main working, and give every feature or fix its own branch.
  • A conflict isn't an error. Decide, delete every marker, git add, continue. Or --abort to back out.
  • Rebase your own work, never shared work. Rebasing gives commits new hashes.
  • Clean up commits before pushing, with interactive rebase.
  • Force-push only with --force-with-lease, never plain --force.
  • Stash with -u -m, and for longer breaks, prefer a WIP commit.