ThisCalculator

Everyday

How to Merge Two Branches on Git

Updated · Standard formula, shown below

Merging branches in Git lets you combine the work from one branch into another, typically bringing a feature branch back into the main branch. You’ll need a working Git installation and a repository with at least two branches that have diverged. This guide covers the standard merge workflow, including how to handle conflicts.

What You’ll Need

  • Git installed on your computer (check with git --version).
  • A Git repository with two or more branches.
  • Write access to the repository (local or remote).
  • Basic familiarity with the command line or a Git GUI.

Step-by-Step

  1. Check your current status. Run git status to see which branch you’re on and if you have uncommitted changes. If you have uncommitted changes, commit them or stash them with git stash before merging.
  2. Fetch the latest changes (if working with a remote). If your repository has a remote (like GitHub), run git fetch origin to get the latest commits from the remote without merging them yet. This ensures you’re merging with the most recent version.
  3. Switch to the branch you want to merge into. This is usually main or master. Run git checkout main (or git switch main).
  4. Make sure your branch is up to date. If you have a remote, run git pull origin main to incorporate any changes that others have pushed. If you’re working locally only, skip this step.
  5. Start the merge. Run git merge <branch-name> where <branch-name> is the branch you want to bring in (e.g., git merge feature-login). Git will attempt to combine the histories automatically.
  6. Handle the merge commit. If the merge is clean (no conflicts), Git will open your default text editor with a pre-filled commit message (e.g., “Merge branch ‘feature-login'”). Save and close the editor to complete the merge. If you prefer not to use the editor, use git merge --no-edit <branch-name> to accept the default message.
  7. Verify the merge. Run git log --oneline to see the new merge commit and confirm the history looks correct. You can also run git status to ensure everything is clean.
  8. Push the merged branch (if remote). If you’re working with a remote, push the updated branch: git push origin main.
  9. Delete the merged branch (optional). Once the merge is successful, you can delete the feature branch locally with git branch -d <branch-name> and remotely with git push origin --delete <branch-name>.

Common Mistakes

  • Merging while you have uncommitted changes. This can cause conflicts or a messy working tree. Always commit or stash before merging.
  • Merging into the wrong branch. Double-check that you’re on the branch you want to receive the changes. Use git branch to see the current branch (the one with an asterisk).
  • Forgetting to pull before merging. If your branch is behind the remote, you might get unexpected conflicts or merge commits. Always pull the latest changes first.
  • Using git merge instead of git rebase when you want a linear history. Merge creates a merge commit, while rebase rewrites history. If you prefer a clean linear history, use rebase, but be careful with shared branches.
  • Deleting the branch before the merge is verified. Even after a successful merge, it’s good practice to run tests before deleting the branch.

Tips

  • Use a GUI or a tool like gitk to visualize the branch structure. This helps you understand what the merge will do before you do it.
  • Keep your feature branches short-lived. The longer a branch lives, the more likely it is to have conflicts when merging.
  • Write clear commit messages. When you merge, the default message includes the branch name, but you can add more context if needed.
  • Use --no-ff to force a merge commit even if a fast-forward is possible. This preserves the history of the feature branch, which can be useful for tracking when a feature was integrated.
  • If you’re unsure about a merge, create a backup branch first. Run git branch backup-main before merging, so you can easily revert if something goes wrong.

Troubleshooting

If the merge does not go as planned, here’s what to do:

  • Merge conflicts. If Git reports conflicts, it will list the files that need attention. Open each file, look for the conflict markers (<<<<<<<, =======, >>>>>>>), and manually edit to keep the correct lines. After resolving all conflicts, run git add <file> for each resolved file, then git commit to complete the merge.
  • Merge aborted. If you realize you made a mistake during the merge, run git merge --abort to go back to the state before the merge. This works only if you haven’t committed the merge yet.
  • Merge commit message editor opens unexpectedly. If you don’t want to use the editor, set the editor to true or use --no-edit in future merges. To exit the editor, type :q (for Vim) or :q! if you want to abort the commit.
  • You merged the wrong branch. If you haven’t pushed yet, you can reset the branch to its previous state with git reset --hard HEAD~1 (assuming the last commit was the merge). If you’ve pushed, you may need to revert the merge with git revert.
  • Git says “Already up to date.” This means the branch you’re merging is already an ancestor of the current branch, so there’s nothing to merge. This can happen if you’ve already merged it or if the branch hasn’t diverged.

Frequently Asked Questions

What is the difference between merge and rebase?

Merge creates a new commit that combines the histories of two branches, preserving the exact timeline. Rebase rewrites the commits of one branch on top of another, creating a linear history. Merge is safer for shared branches; rebase is cleaner for local work.

Can I merge a branch without committing?

No, a merge always creates a commit (unless it’s a fast-forward, which doesn’t need a new commit). If you have uncommitted changes, you must commit or stash them first.

How do I undo a merge?

If the merge hasn’t been pushed, use git reset --hard HEAD~1 to move the branch back. If it has been pushed, use git revert -m 1 <merge-commit-hash> to create a new commit that undoes the merge.

What is a fast-forward merge?

When the branch you’re merging is directly ahead of the current branch (no divergence), Git simply moves the pointer forward, creating no new commit. This is a fast-forward merge. To force a merge commit, use --no-ff.

Why do I get conflicts every time I merge?

Conflicts happen when the same lines of code are changed in both branches. To reduce conflicts, merge frequently, keep branches short-lived, and communicate with teammates about which files you’re editing.

Can I merge a branch without checking out the target branch?

Yes, you can use git merge <branch> <other-branch> from any branch, but it’s generally clearer to check out the target branch first. The command is git merge <target-branch> <source-branch>.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.