Skip to main content
Back to module
Git & the Command Line · Git Basics

Branches and merging

Reading49mLesson 3 of 3 in this module

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 label

commit -am stages and commits every change to already tracked files in one step (new files still need git add).

Fast-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 commit with two parents that combines them.

See the shape of history with:

$ git log --oneline --graph --all

Merge 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 conflict. 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 <<<<<<< HEAD and ======= is your current branch's version (main).
  • Between ======= and >>>>>>> change-time is the incoming version.

Resolve it:

  1. Decide what the line should say. Keep one side, the other, or write a combination. Talk to the other person if unsure.
  2. Delete all three marker lines, leaving clean content: Meeting time: 11:00.
  3. 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 editor

Editors 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 main into 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 branch or git status first.
  • Committing a file that still contains <<<<<<< markers. Search for them before git add.
  • Panicking at a conflict and deleting the repository. Use git merge --abort instead.
  • Deleting an unmerged branch with git branch -D and losing its work. Plain -d refuses if the branch is not merged.

Quick recap

  • A branch is a movable pointer; git switch -c name creates one.
  • git merge branch brings 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, or git merge --abort.

Try it

Open the Do tab to branch, merge and resolve a conflict on purpose.

Also in this lesson: