Git Foundations / Branch and Merge

Merge branches

Lesson 15 of 33. Open to read. No account needed.

Bringing Work Together

Branches let work grow separately. Merging brings it back together: git merge <branch> takes the commits on another branch that your current branch does not have yet and combines them into the current branch.

You always merge into the branch you are on. To bring events into main, switch to main first, then run git merge events. The other branch does not change.

Fast-Forward or Merge Commit

What Git does depends on the shape of the history.

Fast-forward: main points at commit N and events at E, whose parent is N; merging moves main forward to E with no new commit. Three-way merge: main points at E and recipes at R, both children of N; merging creates a commit M with two parents, E and R, and moves main to M.

Fast-forward. In the top half of the diagram, main has no commits of its own since events started from it. Everything on main is already part of events, so there is nothing to combine: Git moves main forward to E. No new commit is made, and Git reports Fast-forward.

Three-way merge. In the bottom half, both branches have new commits since they split at N. Git compares three snapshots: the two branch tips, E and R, and the commit where they split, N, called the merge base. It works out what each side changed since the merge base, combines both sets of changes and records the result in a new merge commit, M. A merge commit has two parents: the previous tip of your branch and the tip of the branch you merged. Git reports Merge made by the 'ort' strategy., naming the method it used.

For a merge commit, Git needs a message. It prepares one, Merge branch 'recipes', and on a terminal opens your editor with it: save and close to accept. --no-edit accepts the prepared message without the editor, and -m "<message>" supplies your own.

When the two sides changed the same lines differently, Git cannot combine them on its own and stops with a conflict. The next lesson covers that.

Choosing to Always Record a Merge

Some teams want every merged branch to appear as a merge commit, even when a fast-forward was possible, so the history shows where each piece of work came in. --no-ff asks for that:

git merge --no-ff <branch>

Before You Merge

  • Be on the branch that should receive the work. git branch shows it.
  • Start from a clean git status. Git refuses to merge when uncommitted edits would be overwritten, and the merge documentation warns that merging with other uncommitted changes can leave you in a state that is hard to back out of.
  • Afterwards, read git log --oneline --graph --all to see what happened.

Exercise: A Fast-Forward and a Three-Way Merge

A newsletter has two branches ready: events and recipes. You will merge both into main and compare the results.

Starting state: Git configured as in the configuration lesson.

  1. Run the setup. It creates main with one commit and two branches, each with one commit of its own:

    mkdir -p ~/git-practice/merge-practice
    cd ~/git-practice/merge-practice
    git init -b main
    cat > newsletter.md <<'EOF'
    # Newsletter
    
    Hello, readers.
    EOF
    git add newsletter.md
    git commit -m "Add newsletter draft"
    git switch -c events
    cat > events.md <<'EOF'
    # Events
    
    Picnic on Saturday.
    EOF
    git add events.md
    git commit -m "Add events"
    git switch main
    git switch -c recipes
    cat > recipe.md <<'EOF'
    # Recipe of the month
    
    Lemon rice.
    EOF
    git add recipe.md
    git commit -m "Add recipe"
    git switch main
    
  2. Look at the starting shape and predict what each merge will do:

    git log --oneline --graph --all
    
    * 7898148 (events) Add events
    | * 7dfa55b (recipes) Add recipe
    |/
    * 8653a21 (HEAD -> main) Add newsletter draft
    

    main has no commits of its own, so merging either branch first can fast-forward.

  3. Merge events:

    git merge events
    git log --oneline --graph --all
    

    Git prints Updating, the two short IDs, and Fast-forward. The graph shows (HEAD -> main, events) on "Add events": main simply moved.

  4. Merge recipes. Now both sides have commits the other lacks, so Git makes a merge commit. Your editor opens with Merge branch 'recipes'; save and close it.

    git merge recipes
    git log --oneline --graph --all
    git show --stat
    ls
    

    Git prints Merge made by the 'ort' strategy. Illustrative graph:

    *   eb69636 (HEAD -> main) Merge branch 'recipes'
    |\
    | * 7dfa55b (recipes) Add recipe
    * | 7898148 (events) Add events
    |/
    * 8653a21 Add newsletter draft
    

    git show --stat includes a Merge: line with two short IDs: the merge commit's two parents. ls lists all three files.

  5. Merge recipes again:

    git merge recipes
    

    Git says Already up to date.: main already contains every commit on recipes.

Success: the graph shows the events merge as a straight line with no merge commit, and the recipes merge as a merge commit with two parents.

If something differs:

What you seeLikely causeNext step
An editor opened during git mergeGit is asking for the merge commit's messageSave and close to accept it
merge: recipe - not something we can mergeA misspelled branch nameCheck the name with git branch
Your local changes to the following files would be overwritten by mergeUncommitted edits to a file the merge changesCommit or discard them, then merge
The merge brought work into the wrong branchYou were on a different branchgit reset --hard ORIG_HEAD undoes an unshared merge; check git status first
CONFLICTBoth branches changed the same linesRun git merge --abort for now; the next lesson resolves conflicts
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf merge-practice
    ls
    

Independent task: before cleaning up, create a branch named credits from main, commit a file credits.md on it, switch back to main and merge it so that a merge commit is recorded even though a fast-forward is possible. Show from the graph that it worked.

Check Your Understanding

Predict whether each merge will fast-forward.

  1. You created feature from main, made two commits on feature, and nobody has committed to main since. On main, you run git merge feature.
  2. After you created feature, a teammate's fix was committed to main. On main, you run git merge feature.
  3. On main, you run git merge feature a second time with no new commits on either branch.

Answer and Reasoning

Independent task

git switch -c credits
cat > credits.md <<'EOF'
Thanks to everyone who wrote for this issue.
EOF
git add credits.md
git commit -m "Add credits"
git switch main
git merge --no-ff credits
git log --oneline --graph --all

Without --no-ff, this merge would fast-forward, because main has no commits that credits lacks. With it, the graph shows a new Merge branch 'credits' commit on main and a side line through "Add credits": a merge commit with two parents.

Understanding check

  1. Fast-forward. main has no commits of its own since feature started, so Git just moves main to feature's tip.
  2. Merge commit. Both branches now have commits the other lacks, so Git combines them from their merge base and records a commit with two parents.
  3. Neither: Git reports Already up to date., because main already contains everything on feature.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy