Merge a Branch
git switch main
git pull
git merge feature/login
The first two lines put you on an up-to-date main; the third merges the feature branch into it. Then push main as usual. The direction matters: the branch you are on receives the changes, and the branch you name stays as it is.
Fast-Forward or Merge Commit
If main has no new commits since the feature branch started, Git does not need to combine anything. It moves main forward to the last commit of the feature branch. This is a fast-forward, and it creates no merge commit.
If both branches have new commits, Git creates a merge commit with two parents that joins the two lines of history. You can choose the behavior:
| Command | Result |
|---|---|
git merge dev | Fast-forward when possible |
git merge --no-ff dev | Always a merge commit |
git merge --ff-only dev | Fast-forward or stop |
git merge --squash dev | One staged change |
--no-ff keeps a visible record that a group of commits arrived together as one feature, which some teams want on main.
Keep a Feature Branch Up to Date
On a long-running feature branch, merge main into it from time to time, so conflicts appear in small pieces:
git switch feature/login
git merge main
When the feature is done, merge it back the other way, from main. Rebasing the feature branch onto main is the other way to stay current; the trade-offs are in Git merge vs rebase.
Squash Merge
git merge --squash feature/login
git commit -m "Add login form"
All changes of the branch are staged as one change, and you commit them yourself. History on main shows one commit instead of every small step. Git does not record the branch as merged, so git branch -d later refuses to delete it; use -D once you have checked. Other ways to combine commits are in how to squash commits in Git.
Conflicts and Aborting
If both branches changed the same lines, the merge stops and lists the conflicting files. Fix them, stage them, and finish:
git status
git add src/login.js
git merge --continue
The full procedure is in how to resolve a merge conflict in Git. To cancel the merge and return to the state before it started:
git merge --abort
Start a merge with a clean working tree: commit or stash your edits first, so an abort has nothing of yours to lose.
List Merged Branches
git branch --merged main
git branch --no-merged main
The first list shows branches whose commits are all in main and can be deleted; the second shows branches with work not merged yet. Cleanup is covered in Git delete branch.
Undo a Merge
Before you push, you can move the branch back to where it was. Warning: reset --hard also discards uncommitted changes in tracked files, so check git status first:
git reset --hard ORIG_HEAD
Git sets ORIG_HEAD to the previous position of the branch during a merge. If another command has changed it since, use the commit hash from git reflog.
After you push, do not rewrite the branch. Revert the merge commit and keep the first parent, the branch you merged into:
git revert -m 1 9f8e7d6
git push
The details of reverting a merge, and why merging the same branch again later does not bring its changes back, are in Git revert: undo a commit safely.