Branching and merging
Branches let you try new work without breaking the version that already works. In Git a branch is not a copy of your files: it is a small movable label that points at one commit. Watch the labels move as you create a branch, commit on it, merge it back (by a fast-forward or with a merge commit), fix a merge conflict, and share your work through a remote with push, pull and a pull request.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01You are on the branch feature and make a new commit. What moves?
HEAD is attached to feature, so only feature moves forward. Other branches keep pointing where they were.
02main points at B. feature points at C, and C's parent is B. What does git merge feature do while you are on main?
main is simply behind on the same line of history, so Git only moves the main pointer forward to C.
03Both branches changed the same line of app.py and you merge. What happens?
Git cannot choose for you. Edit the file to what it should be, remove the markers, then git add and git commit to make the merge commit.
04Which statement about git push and git pull is true?
push sends commits to the remote and moves its branch; pull is a fetch followed by a merge (or a rebase if configured). A pull request is a review page on a site such as GitHub.
05You already pushed feature and a teammate has started work on top of it. Should you rebase feature now?
Rebase replays commits as new ones (D became D′ with a new id). That is fine for work only you have, but never rebase commits you have already shared: others' work would point at the old ones.
Glossary
- Commit
- A saved snapshot of the project, with a message and a link back to its parent commit (two parents for a merge commit).
- Branch
- A movable name that points at one commit. Committing on a branch moves it forward to the new commit.
- HEAD
- Git's marker for where you are: normally it points at the branch you have switched to.
- Fast-forward
- A merge where the current branch is simply behind, so Git just moves its pointer forward. No new commit is made.
- Merge commit
- A commit with two parents that joins two lines of work that had split.
- Merge conflict
- When both sides changed the same lines, Git stops and marks the clash in the file for you to resolve.
- Rebase
- Replaying your commits on top of another branch as new commits, giving a straight history. Do not rebase commits others already have.
- Remote and pull request
- A remote (usually origin) is a shared copy of the repository on a server. A pull request is a page on a hosting site where others review a branch before it is merged.
Notes and sources
- Commit ids in the examples (such as 7b9e41d) are made up and shortened. Real ids are 40-character hashes, so yours will differ.
- A branch is stored as a small file in .git/refs/heads holding one commit id (Git may also pack many of them into one file, .git/packed-refs). That is why making a branch is instant.
- git switch arrived in Git 2.23 (2019). Older guides use git checkout -b feature, which does the same thing.
- Git normally refuses to switch branches if uncommitted changes would be overwritten. Commit your work first, or stash it: git stash puts unfinished changes aside, and git stash pop brings them back.
- A rebased commit gets a new id because its parent changed. If D and E clash, rebase stops with the same conflict, and you resolve it, git add, then git rebase --continue.
- git pull is git fetch followed by a merge (or a rebase, if configured). Recent Git versions ask you to choose when the branches have split and a fast-forward is not possible.
- A pull request needs commits that main does not have yet. That is why G is made first: a pull request from a branch that is already merged would have nothing to review.
- Pull requests belong to hosting sites such as GitHub, GitLab (where they are called merge requests) and Bitbucket, not to Git itself.
Quiz Unlocks after the last step.