Module 4 Lesson 3: Merging branches
·DevOps

Module 4 Lesson 3: Merging branches

Bring the team together. Learn how to merge a feature branch back into 'main' and understand the basic workflow for combining disconnected histories.

Module 4 Lesson 3: Merging branches

Branching is how we separate our work; merging is how we put it back together. Merging takes the history of one branch and integrates it into another.

In this lesson, we learn the standard workflow for merging a completed feature back into your main branch.


1. The Standard Workflow

When you want to merge feature-a into main, you must always follow these steps:

Step 1: Switch to the Target Branch

You must be on the branch that you want to receive the changes.

git switch main

Step 2: Merge the Source Branch

Tell Git to bring the changes from the other branch into your current one.

git merge feature-a

Step 3: Cleanup (Optional)

Once the changes are merged, you usually don't need the feature branch anymore.

git branch -d feature-a

2. What happens during a merge?

Git looks at the commit history of both branches and finds the common ancestor (the point where they diverged). It then takes all the changes that happened on feature-a since that ancestor and applies them to main.

graph LR
    C1["Commit 1 (Ancestor)"] --> C2["Commit 2 (Main)"]
    C1 --> F1["Commit A (Feature)"]
    F1 --> F2["Commit B (Feature)"]
    
    C2 --> M["Merge Commit (Combined)"]
    F2 --> M

3. Merging is mostly automatic

Most of the time, Git is smart enough to handle merging automatically.

  • If you were working on the footer in feature-a and someone else was working on the header in main, Git will simply combine the two.
  • You will see a message saying: "Automatic merge went well; stopped before committing." or a text editor will pop up asking for a "Merge Commit Message."

4. Abortion!

If you start a merge and something goes wrong (or you realize you're on the wrong branch), you can always cancel the operation:

git merge --abort

This returns your repository to the state it was in before you tried to merge.


Lesson Exercise

Goal: Merge your first branch.

  1. Create a branch called fix-typo.
  2. Edit a file in fix-typo to fix a word, and commit it.
  3. Switch back to main.
  4. Merge fix-typo into main.
  5. Run git log --oneline --graph. Can you see where the branch diverged and then came back into the main line?

Observation: You'll notice that your main branch now contains the typo fix as if you had committed it there directly (or via a special "merge commit").


Summary

In this lesson, we established:

  • You must always be on the "receiving" branch before running git merge.
  • Merging is the process of integrating history from one branch into another.
  • git merge --abort is your safety button for failed merges.

Next Lesson: When you merge, Git sometimes does a "Fast-Forward" and sometimes creates a "Three-Way Merge." We’ll explore the difference between the two.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn