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.
| Parameter | Description |
|---|---|
--ff-only | Update the current branch only if the other tip is a descendant of it. Otherwise abort without merging. |
--no-ff | Create 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.
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