git merge

Fast-forward, --no-ff, --ff-only failure, and a conflict aborted in a temporary repository, checked on Git 2.39.5.

コマンドページの本文は現在英語です。

git merge joins another branch into the current one. When the current branch has not moved since the other branch diverged, Git can fast-forward: it moves the branch name to the other tip and does not create a merge commit. --no-ff always records a merge commit. --ff-only refuses to merge unless a fast-forward is possible.

A content conflict stops in the working tree and the index. The conflict and --abort examples run only in a temporary repository. --abort restores the pre-merge state; you can also resolve the files and commit instead. This page does not run merge in the site checkout.

Examples were checked against Git 2.39.5. See the official documentation for the complete command.

Git command catalog → · Git tutorial overview →

Fast-forward, --no-ff, and --ff-only

When topic is a direct descendant of main, git merge --ff-only topic updates main to the topic tip, prints Fast-forward, and leaves the merge commit with a single parent. The same history merged with --no-ff creates a two-parent commit whose subject is the merge message. On Git 2.39.5 that merge reports the ort strategy.

If main and topic have diverged, --ff-only exits 128, prints a fatal error, and leaves HEAD unchanged. The example captures the fatal line with 2>&1. That 128 is not a conflict; the worktree stays clean. A later merge without --ff-only would still need a real merge or a rebase, which this page does not run.

git merge --ff-only, git merge --no-ff

git merge --ff-only <branch>
git merge --no-ff -m <message> <branch>

Fast-forward when possible, always create a merge commit with --no-ff, or refuse a non-fast-forward merge.

ParameterDescription
--ff-onlyUpdate the current branch only if the other tip is a descendant of it. Otherwise abort without merging.
--no-ffCreate a merge commit even when a fast-forward is possible.
-m <message>The merge commit message, used with --no-ff in the example.

Returns: On a successful fast-forward, the current branch points at the other tip. On a successful --no-ff, HEAD is a two-parent merge commit.

Exit status

  • 0 — The fast-forward or the --no-ff merge completed. Measured on Git 2.39.5.
  • 128 — fatal: Not possible to fast-forward, aborting. Measured for a diverged --ff-only merge on Git 2.39.5. This is not a conflict exit.
  • The graph line after the merge commit is a pipe, a backslash, and two trailing spaces. The next join line is a pipe, a slash, and two trailing spaces.
bash
setup() {
  git init -b main >/dev/null
  git config user.name "Practice Author"
  git config user.email "[email protected]"
  git config commit.gpgsign false
  git config color.ui false
  mkdir -p .disabled-hooks
  git config core.hooksPath ".disabled-hooks"
  printf 'base\n' > file.txt
  git add file.txt
  git commit -m "Add file" >/dev/null
  git switch -c topic >/dev/null 2>&1
  printf 'topic\n' > file.txt
  git add file.txt
  GIT_AUTHOR_DATE='2024-01-15T13:00:00+0000' GIT_COMMITTER_DATE='2024-01-15T13:00:00+0000' git commit -m "Edit on topic" >/dev/null
  git switch main >/dev/null 2>&1
}

mkdir ff && cd ff
setup
echo "ff"
git merge --ff-only topic 2>&1
echo "ff-exit=$?"
echo "parents=$(git log -1 --pretty=format:%p)"
echo "subject=$(git log -1 --pretty=format:%s)"
cd ..

mkdir no-ff && cd no-ff
setup
echo "no-ff"
GIT_AUTHOR_DATE='2024-01-15T14:00:00+0000' GIT_COMMITTER_DATE='2024-01-15T14:00:00+0000' git merge --no-ff topic -m "Merge topic"
echo "no-ff-exit=$?"
echo "parents=$(git log -1 --pretty=format:%p)"
echo "subject=$(git log -1 --pretty=format:%s)"
git log --oneline --decorate --graph --all
cd ..

mkdir ff-fail && cd ff-fail
setup
printf 'main\n' > other.txt
git add other.txt
GIT_AUTHOR_DATE='2024-01-15T14:00:00+0000' GIT_COMMITTER_DATE='2024-01-15T14:00:00+0000' git commit -m "Add other on main" >/dev/null
echo "ff-only-fail"
git merge --ff-only topic 2>&1
echo "ff-only-exit=$?"
echo "head=$(git log -1 --pretty=format:%s)"
st=$(git status --porcelain)
if [ -z "$st" ]; then echo "(clean)"; else printf '%s\n' "$st"; fi
cd ..

Expected output

ff
Updating 8bb07df..a87e73e
Fast-forward
 file.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ff-exit=0
parents=8bb07df
subject=Edit on topic
no-ff
Merge made by the 'ort' strategy.
 file.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
no-ff-exit=0
parents=8bb07df a87e73e
subject=Merge topic
*   62ef4ce (HEAD -> main) Merge topic
|\  
| * a87e73e (topic) Edit on topic
|/  
* 8bb07df Add file
ff-only-fail
fatal: Not possible to fast-forward, aborting.
ff-only-exit=128
head=Add other on main
(clean)

Related: git branch, git log

Official documentation →

Conflict and --abort in a temporary repository

When both sides edited the same lines, git merge stops with a content conflict, exits 1, and leaves the path unmerged. Porcelain reports UU, and .git/MERGE_HEAD is present so later commands know a merge is in progress.

git merge --abort returns the branch, the index, and the working tree to the pre-merge state. In the example the file is again main-line, the status is clean, and MERGE_HEAD is gone. Abort is one way out; you can also edit the conflicted file, git add it, and commit the merge. Both the conflict and the abort are demonstrated only in this temporary repository.

git merge, git merge --abort

git merge <branch>
git merge --abort

Stop on a content conflict, then abort the in-progress merge and restore the pre-merge state.

ParameterDescription
--abortCancel the in-progress merge and try to restore the pre-merge HEAD, index, and working tree.

Returns: On a conflict, the merge is left unfinished. After a successful --abort, HEAD and the worktree match the pre-merge commit.

Exit status

  • 1 — Automatic merge failed; fix conflicts and then commit the result. Measured on Git 2.39.5, with UU in porcelain.
  • 0 — git merge --abort completed and cleared MERGE_HEAD. Measured on Git 2.39.5.
  • The Auto-merging and CONFLICT lines are Git's own output on standard output. This page does not resolve the conflict with a one-line "ours" or "theirs" shortcut.
bash
git init -b main >/dev/null
git config user.name "Practice Author"
git config user.email "[email protected]"
git config commit.gpgsign false
git config color.ui false
mkdir -p .disabled-hooks
git config core.hooksPath ".disabled-hooks"
printf 'base\n' > file.txt
git add file.txt
git commit -m "Add file" >/dev/null
git switch -c topic >/dev/null 2>&1
printf 'topic-line\n' > file.txt
git add file.txt
GIT_AUTHOR_DATE='2024-01-15T13:00:00+0000' GIT_COMMITTER_DATE='2024-01-15T13:00:00+0000' git commit -m "Edit on topic" >/dev/null
git switch main >/dev/null 2>&1
printf 'main-line\n' > file.txt
git add file.txt
GIT_AUTHOR_DATE='2024-01-15T14:00:00+0000' GIT_COMMITTER_DATE='2024-01-15T14:00:00+0000' git commit -m "Edit on main" >/dev/null
echo "merge"
git merge topic
echo "merge-exit=$?"
echo "porcelain"
git status --porcelain
if test -f .git/MERGE_HEAD; then echo "MERGE_HEAD=yes"; else echo "MERGE_HEAD=no"; fi
echo "abort"
git merge --abort
echo "abort-exit=$?"
echo "porcelain"
st=$(git status --porcelain)
if [ -z "$st" ]; then echo "(clean)"; else printf '%s\n' "$st"; fi
if test -f .git/MERGE_HEAD; then echo "MERGE_HEAD=yes"; else echo "MERGE_HEAD=no"; fi
echo "head=$(git log -1 --pretty=format:%s)"
echo "file=$(cat file.txt)"

Expected output

merge
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
merge-exit=1
porcelain
UU file.txt
MERGE_HEAD=yes
abort
abort-exit=0
porcelain
(clean)
MERGE_HEAD=no
head=Edit on main
file=main-line

Related: git status

Official documentation →