読み物Git

Level: Basic

Understanding Git Branches, Merging, and Merge Conflicts

What a branch really is, how fast-forward and three-way merges work, and how to resolve a merge conflict calmly, with real output from a small example.

Edeeson Opina7 min read

A Basic lesson card: lesson 4 of the Git & Version Control series, Understanding Git Branches, Merging, and Merge Conflicts, with the Miso fox holding a camera.

What you’ll learn

  • Explain branches as movable pointers and HEAD as the current branch
  • Create, switch, merge, and delete branches
  • Tell fast-forward merges from three-way merges
  • Resolve merge conflicts and verify the result
On this page
  1. Before you start
  2. What a branch really is
  3. Creating and switching branches
  4. Naming branches
  5. Merging
  6. Fast-forward merges
  7. Three-way merges
  8. Merge conflicts
  9. Resolving a conflict
  10. Getting out of a merge
  11. Tools that help
  12. Deleting branches
  13. Reducing conflicts
  14. Common mistakes and troubleshooting
  15. Practical exercise: create and resolve a conflict
  16. Key takeaways
  17. References
  18. What's next

Branches are what make Git feel different from saving copies of files. A branch lets you try an idea, build a feature, or fix a bug on its own line of history, without disturbing the version that works. When the work is ready, you merge it back. This lesson explains what a branch actually is (it's much smaller than most people think), how merging works, and how to resolve a merge conflict calmly, with real output from a small example.

Before you start

You should be comfortable with status, add, commit, and log from Git Fundamentals: Commits, Staging, History, and .gitignore. Use a practice repository with a few commits.

What a branch really is

A branch in Git is a movable name that points at one commit. That's all. Creating a branch doesn't copy any files; it writes a tiny file containing a commit ID. When you commit on a branch, the new commit's parent is the commit the branch pointed to, and the branch moves forward to the new commit.

Git also keeps a special pointer, HEAD, which says which branch you're currently on. Committing moves the branch that HEAD points to.

Because branches are just pointers, creating and switching them is nearly instant, even in huge projects. That's why Git workflows use branches freely: one per feature, one per bug fix, one for an experiment you might throw away.

Creating and switching branches

Shell
git branch                     # list local branches; * marks the current one
git switch -c feature/ramen    # create a branch and switch to it
git switch main                # switch back
git branch -v                  # branches with their latest commit

git switch was added in Git 2.23 to make branch commands clearer. You'll still see the older git checkout -b feature/ramen and git checkout main in many tutorials; they do the same thing here, but checkout also restores files, which makes it easier to misuse.

Switching branches updates your working tree to match the branch. If you have uncommitted changes that would be overwritten, Git refuses to switch and tells you which files are in the way. Commit them, or set them aside with git stash (lesson 10).

Naming branches

Use short, descriptive names, often with a prefix that says what kind of work it is: feature/search-filters, fix/login-redirect, docs/install-guide. Avoid spaces and personal names; the branch describes the work, not the person.

Merging

When the work on a branch is ready, switch to the branch you want to update (usually main) and merge the other branch into it:

Shell
git switch main
git merge feature/ramen

Git combines the two lines of history in one of two ways.

Fast-forward merges

If main hasn't changed since the feature branch was created, there's nothing to combine: the feature branch is simply ahead. Git just moves main forward:

Shell
$ git merge feature/b
Updating e815183..0eeb649
Fast-forward
 b.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 b.md

No new commit is created. History stays a straight line.

Three-way merges

If both branches have new commits, Git finds their merge base (the last commit they share), compares each branch with it, and combines the changes. The result is a new merge commit with two parents:

Shell
$ git log --oneline --graph
*   c2bc458 Merge branch 'feature/ramen'
|\
| * c502972 Add tofu
* | 26142a7 Add wakame
|/
* c7a0a9b Add miso soup
* 123c8d2 Add README and .gitignore

When the two branches changed different files, or different parts of the same file, Git merges automatically. When both changed the same lines differently, Git can't know which version is right, and asks you.

Merge conflicts

Here, main changed the second line of soup.md to "with wakame" while feature/ramen changed the same line to "with tofu". Merging produces a conflict:

Shell
$ git merge feature/ramen
Auto-merging soup.md
CONFLICT (content): Merge conflict in soup.md
Automatic merge failed; fix conflicts and then commit the result.

$ git status --short
UU soup.md

UU means "unmerged, both modified." Git has written both versions into the file, between conflict markers:

Text
Miso soup
<<<<<<< HEAD
with wakame
=======
with tofu
>>>>>>> feature/ramen
  • Between <<<<<<< HEAD and ======= is the version on the branch you're on (main).
  • Between ======= and >>>>>>> feature/ramen is the incoming version.

A conflict isn't an error or a sign that something broke. It's Git being careful: two people made decisions about the same lines, and a human has to decide what the result should be.

Resolving a conflict

  1. Open the file and decide what the correct content is. It might be one side, the other, or a combination. Here, the recipe should have both ingredients.
  2. Remove all the conflict markers.
  3. Stage the resolved file and complete the merge.
Shell
# After editing soup.md to contain "with tofu and wakame"
git add soup.md
git commit --no-edit    # uses the prepared message "Merge branch 'feature/ramen'"

Before committing, make sure no markers are left anywhere:

Shell
git diff --check        # flags leftover conflict markers and whitespace problems

Then run your tests or build. A merge can be textually clean and still wrong: one branch renamed a function while the other added a new call to the old name, and Git merges both without complaint. Only tests catch that.

Getting out of a merge

If a merge goes badly and you want to start over, return to the state before you ran git merge:

Shell
git merge --abort

Tools that help

  • Most editors, including VS Code and JetBrains IDEs, show conflicts with buttons to accept either side or both.
  • git mergetool opens a configured three-way merge tool.
  • Setting git config --global merge.conflictStyle zdiff3 adds the merge base's version between the markers, which often makes it obvious what each side changed.

Deleting branches

After a branch is merged, delete it to keep the list short:

Shell
$ git branch -d feature/b
Deleted branch feature/b (was 0eeb649).

-d refuses to delete a branch whose commits aren't merged anywhere, which protects you from losing work. -D forces it. If you force-delete by mistake, the commits can usually still be found through the reflog (lesson 9).

Reducing conflicts

You can't eliminate conflicts, but you can make them rarer and smaller:

  • Merge often. Short-lived branches drift less from main. Bring main into a long-running branch regularly.
  • Keep commits focused. A commit that reformats a whole file conflicts with everyone else's work in it. Do formatting changes separately and announce them.
  • Use a shared formatter. When everyone's editor formats code the same way, whitespace and style differences stop causing conflicts.
  • Talk to each other. If two people are about to restructure the same module, a short conversation beats a long conflict.

Common mistakes and troubleshooting

  • Committing to the wrong branch. Check git status (it names the branch) before committing. To move the commit, see lesson 9.
  • Leaving conflict markers in a file. Search for <<<<<<< and run git diff --check before committing.
  • Resolving by blindly taking one side. You may discard a teammate's work. Read both sides.
  • Merging in the wrong direction. git merge X brings X into the branch you're on. Switch first.
  • "Your local changes would be overwritten." Commit or stash before switching or merging.
  • Huge, long-lived branches. They produce huge conflicts. Merge or integrate often (lesson 8).

Practical exercise: create and resolve a conflict

  1. In a practice repository on main, commit a file menu.md with three lines.
  2. Create feature/spicy and change line 2; commit.
  3. Switch to main and change line 2 differently; commit.
  4. Merge feature/spicy into main. Read the conflict markers and explain which side is which.
  5. Resolve it with a combination of both, check with git diff --check, and commit.
  6. Run git log --oneline --graph and identify the merge commit and its two parents.
  7. Repeat the setup, start the merge, and this time use git merge --abort. Confirm with git status that nothing changed.

Key takeaways

  • A branch is a movable pointer to a commit; HEAD says which branch you're on.
  • git switch -c name creates and switches; git merge name brings a branch into the current one.
  • Fast-forward merges just move the pointer; three-way merges create a merge commit with two parents.
  • Conflicts happen when both sides changed the same lines; resolve them, remove the markers, stage, commit, and test.
  • Short-lived branches and focused commits keep conflicts small.

References

What's next

Everything so far has been on one computer. Working with Remote Repositories: Clone, Fetch, Pull, Push, and SSH connects your repository to others and sets up secure authentication.

See every lesson in Git & Version Control: From Fundamentals to Advanced Workflows