Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Merging and Rebasing

git merge command

Reference for git merge: fast-forward and merge commits, --no-ff, --squash, --abort, conflicts, merge messages, strategies and the options you will actually use.

git merge brings the history of another branch into the branch you are on. It finds the merge base (the last commit both lines share), combines the two sets of changes made since then, and records the result. If your branch has not moved since the other one split off, there is nothing to combine and Git simply slides your branch pointer forward: a fast-forward. Otherwise it creates a merge commit with two parents.

Both histories stay intact, which is why merging is the safe default for shared branches. Conflicts happen when both sides changed the same lines; Git stops, marks the files, and waits for you to resolve them and commit, or to abort and return to where you were.

Terminal
git switch main
git merge feature            # fast-forward if possible, merge commit otherwise
git merge --no-ff feature    # always create a merge commit
git merge --abort            # give up a conflicted merge

Try it

Bash Fast-forward versus merge commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Conflicts, abort and squash

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git merge [<options>] [<commit>...]
git merge --abort | --continue | --quit
Name Description
<commit> One or more branches or commits to merge into the current branch. Several commits make an octopus merge.
--ff Fast-forward when possible (the default).
--no-ff Always create a merge commit, even when a fast-forward is possible.
--ff-only Refuse unless the merge can fast-forward.
--squash Stage the combined changes but do not commit or record a second parent.
--no-commit Perform the merge but stop before committing, so you can inspect or edit.
-m <msg> Set the merge commit message. Repeat for several paragraphs.
--no-edit / --edit Accept the generated message without or with opening an editor.
-s <strategy>, -X <option> Choose a merge strategy and strategy options such as ours, theirs or ignore-space-change.
--abort Undo an in-progress conflicted merge and restore the pre-merge state.
--continue Finish the merge after resolving conflicts (same as git commit).
--quit Forget the merge in progress but leave the working tree and index as they are.
--autostash Stash local changes before merging and re-apply them afterwards.
--log[=<n>] Add up to n one-line summaries of merged commits to the message.
-S[<key-id>] Sign the merge commit.
--allow-unrelated-histories Allow merging branches with no common ancestor.
--verify-signatures Refuse to merge unless the tip commit has a valid signature.

Exit status: 0 on a clean merge, 1 when conflicts remain.

Usage notes

  • Start clean. Merge with a clean working tree. Uncommitted changes in files the merge touches make Git refuse; --autostash is the convenient way around it.
  • Know which side is which. During a merge "ours" is the branch you are on and "theirs" is the one you named. Marker blocks show <<<<<<< HEAD (ours) above ======= and >>>>>>> blue (theirs).
  • pull is fetch plus merge. git pull runs this command; pull.ff only or merge.ff change the default behavior.
  • Config. merge.ff (false is --no-ff for all merges), merge.conflictStyle (diff3 or zdiff3 also shows the base), merge.log, merge.autoStash, and branch.<name>.mergeOptions for one branch.
  • Annotated tags are merged by their commit; Git opens an editor with the tag message so you can keep it.
  • Squash loses ancestry. After --squash Git does not know the branch was merged, so a later real merge or git branch -d treats it as unmerged. Delete with -D or use a normal merge.
  • Resolved wrong? Before pushing, git reset --hard ORIG_HEAD undoes the last merge. See git reset.
  • Reusing resolutions. git rerere remembers how you resolved a conflict and replays it.
  • Big picture. To pick a strategy see Merge strategies; to find the common ancestor yourself use git merge-base.