What you'll learn
- Branch, merge and resolve a simple conflict
- Create and switch branches with git switch
- Tell a fast-forward merge from a merge commit
- Back out of a merge safely with git merge --abort
Create a branch, merge it back into main, and resolve a simple merge conflict calmly.
Why it matters
Real work rarely moves in a straight line. You start a feature, a bug report arrives, a teammate is changing the same file. Branches let several lines of work exist side by side without getting in each other's way, and merging brings them back together. Every team workflow on GitHub (next module) is built on branches.
The core idea
A branch is just a named pointer to a commit. main points at the latest commit on the main line. When you create a branch and commit on it, only that branch's pointer moves. HEAD means "the branch you are on now".
$ git branch # list branches; * marks the current one
* main
$ git switch -c add-summary # create a branch and switch to it
$ git branch
* add-summary
main(git switch needs Git 2.23+. Older guides use git checkout -b add-summary, which does the same.)
Work on the branch, then merge
$ echo "## Summary" >> README.md
$ git commit -am "Add summary section to README"
$ git switch main # README.md goes back to main's version
$ git merge add-summary
Updating 9e02c55..c41d7a2
Fast-forward
README.md | 1 +
$ git branch -d add-summary # delete the merged branch labelcommit -am stages and commits every change to already tracked files in one step (new files still need git add).
Fast-forwardA merge where the target branch had no new commits, so Git just moves its pointer forward.: if main had no new commits since you branched, Git just moves main's pointer forward. If both branches have new commits, Git creates a merge commitA commit with two parents that combines branches which both have new commits. with two parents that combines them.
See the shape of history with:
$ git log --oneline --graph --allMerge conflicts
If both branches changed the same lines of the same file, Git cannot know which version is right, so it stops and asks you. That is a merge conflictWhat happens when both branches changed the same lines; Git stops and asks you to choose.. It is normal, not an error in your work.
Worked example
On main, plan.md says Meeting time: 10:00. You create a branch change-time and change it to 11:00 and commit. Meanwhile, back on main, someone changes it to 09:30 and commits. Now:
$ git switch main
$ git merge change-time
Auto-merging plan.md
CONFLICT (content): Merge conflict in plan.md
Automatic merge failed; fix conflicts and then commit the result.Open plan.md:
<<<<<<< HEAD
Meeting time: 09:30
=======
Meeting time: 11:00
>>>>>>> change-time- Between
<<<<<<< HEADand=======is your current branch's version (main). - Between
=======and>>>>>>> change-timeis the incoming version.
Resolve it:
- Decide what the line should say. Keep one side, the other, or write a combination. Talk to the other person if unsure.
- Delete all three marker linesThe <<<<<<<, ======= and >>>>>>> lines Git writes around the two versions; delete them when resolving., leaving clean content:
Meeting time: 11:00. - Mark it resolved and finish the merge:
$ git status # lists "both modified: plan.md"
$ git add plan.md
$ git commit # Git suggests a merge message; save and close the editorEditors such as VS Code highlight conflicts with buttons like "Accept Current Change" and "Accept Incoming Change", which edit the markers for you.
If you get lost, git merge --abort puts everything back to how it was before the merge.
Habits that prevent pain
- Keep branches short-lived and focused: one feature or fix each.
- Merge
maininto your branch (or pull often) so you find conflicts early, when they are small. - Name branches clearly:
fix-total-rounding,add-login-page. - Commit before switching branches. Git refuses to switch if uncommitted changes would be overwritten, and tells you so.
Common mistakes
- Committing to the wrong branch. Check
git branchorgit statusfirst. - Committing a file that still contains
<<<<<<<markers. Search for them beforegit add. - Panicking at a conflict and deleting the repository. Use
git merge --abortinstead. - Deleting an unmerged branch with
git branch -Dand losing its work. Plain-drefuses if the branch is not merged.
Quick recap
- A branch is a movable pointer;
git switch -c namecreates one. git merge branchbrings changes into the current branch: fast-forward or a merge commit.- Conflicts happen when both sides change the same lines: edit, remove markers,
git add,git commit, orgit merge --abort.
Try it
Open the Do tab to branch, merge and resolve a conflict on purpose.
Key terms (5)
- HEAD
- Git's name for the branch you are on now.
- fast-forward
- A merge where the target branch had no new commits, so Git just moves its pointer forward.
- merge commit
- A commit with two parents that combines branches which both have new commits.
- merge conflict
- What happens when both branches changed the same lines; Git stops and asks you to choose.
- marker lines
- The <<<<<<<, ======= and >>>>>>> lines Git writes around the two versions; delete them when resolving.