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
- What a branch really is: a named pointer to one commit, stored in a 41-byte file. HEAD points to the current branch.
- Branch commands:
git switch -ccreates and switches,-mrenames,-ddeletes. - Merging: a fast-forward slides the branch forward; a three-way merge makes a merge commit with two parents.
- Merge conflicts: edit the file, delete the markers,
git add, then commit. - Rebasing: replays a branch on top of another for a straight history, with new hashes.
- Interactive rebase: reword, squash, fixup, drop, reorder or edit commits before sharing them.
- Stash and cherry-pick: stash puts unfinished work aside; cherry-pick copies one commit to another branch.
Key terms
| Term | Meaning |
|---|---|
| Branch | A name that points to one commit and moves forward with each new commit |
HEAD | A pointer to the current branch |
| Detached HEAD | HEAD pointing straight at a commit instead of a branch |
| Common ancestor | The last commit two branches share |
| Merge | Bringing another branch's work into the current branch |
| Fast-forward | A merge that just slides the branch forward, with no new commit |
| Three-way merge | A merge that combines both sides from their common ancestor |
| Merge commit | A commit with two parents, made by a three-way merge |
| Merge conflict | Both branches changed the same lines, so a person has to decide |
| Conflict markers | The <<<<<<<, ======= and >>>>>>> lines Git writes around a conflict |
| Ours / theirs | The current branch's version / the incoming one (swapped during a rebase) |
| Rebase | Replaying a branch's commits on top of another branch |
| Interactive rebase | Editing a list of recent commits to clean up the history |
| Squash / fixup | Combining a commit into the one before it, with or without its message |
ORIG_HEAD | Where the branch was before a merge or rebase, for undoing it |
| Stash | A drawer for unfinished changes |
| Cherry-pick | Copying one commit onto the current branch, with a new hash |
Essential commands
| Command | What it does |
|---|---|
git branch | Lists branches |
git switch -c name | Creates a branch and switches to it |
git switch name | Switches to a branch |
git switch - | Goes back to the previous branch |
git branch -m new | Renames the current branch |
git branch -d name | Deletes a merged branch |
git merge branch | Merges a branch into the current one |
git merge --abort | Cancels a merge in progress |
git rebase main | Replays the current branch on top of main |
git rebase --continue | Goes on after fixing a conflict |
git rebase -i HEAD~N | Cleans up the last N commits |
git stash -u -m "…" | Puts all changes aside, with a label |
git stash pop | Brings the newest stash back |
git cherry-pick hash | Copies one commit here |
git log --oneline --graph --all | Draws every branch |
Rules to remember
- A merge goes into the current branch. Switch to the branch that receives the work first.
- Keep
mainworking, and give every feature or fix its own branch. - A conflict isn't an error. Decide, delete every marker,
git add, continue. Or--abortto 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.