Skip to main content

Git Branch Merge Guide

When to Use

When you've finished development on a feature branch and want to merge it back into a target branch like main, master, or develop, follow the steps on this page.

The core principle is one sentence: switch to the target branch first, then run merge to bring the source branch in.

Check Current State First

git status
git branch --show-current
git log --oneline --decorate --graph -10
  • git status -- confirm the working directory is clean first.
  • git branch --show-current -- confirm which branch you're currently on.
  • In a team collaboration environment, you should usually sync the latest remote state before merging.

Merge a Feature Branch into the Target Branch

git switch develop
git merge feature/login

If you want to force an explicit merge commit:

git switch develop
git merge --no-ff feature/login

Common Scenarios

Normal Merge Flow

git switch develop
git merge feature/login

Merge Conflict

git status

Edit the conflicted file and remove conflict markers:

<<<<<<< HEAD
=======
>>>>>>> feature/login

After confirming the content, continue:

git add conflicted-file.txt
git commit

Choose Current Branch or Source Branch Version Directly

git checkout --ours conflicted-file.txt
git checkout --theirs conflicted-file.txt
git add conflicted-file.txt
git commit

In a merge scenario:

  • --ours refers to the current branch, i.e., the target branch version.
  • --theirs refers to the source branch being merged in.

Abort the Merge

git merge --abort

Risks and Boundaries

  • Merge commands always operate on the "current branch," so switching to the target branch first is critical.
  • It's best to have a clean working directory before merging, otherwise conflict resolution becomes more chaotic.
  • git merge --abort only works while the merge is still in progress; once the merge commit is made, you can't use it to roll back.
  • The meaning of --ours / --theirs is tied to the "current merge direction" -- check which branch you're on first.
git diff develop..feature/login
git log --oneline --decorate --graph -10
git merge --ff-only feature/login
git push origin develop