git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v5 5/6] pull: add merge-ff-only option

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 29, 2014, 11:17 UTC
Message-ID
<1398770226-9686-6-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1398770226-9686-1-git-send-email-felipe.contreras@gmail.com>

It is very typical for Git newcomers to inadvertently create merges and worst; inadvertently pushing them. This is one of the reasons many experienced users prefer to avoid 'git pull', and recommend newcomers to avoid it as well.

To avoid these problems and keep 'git pull' useful, it has been suggested that 'git pull' barfs by default if the merge is non-fast-forward, which unfortunately would break backwards compatibility.

This patch leaves everything in place to enable this new mode, but it only gets enabled if the user specifically configures it; pull.mode = merge-ff-only.

Later on this mode can be enabled by default (e.g. in v2.0).
For the full discussion you can read:
http://thread.gmane.org/gmane.comp.version-control.git/225146/focus=225305
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/config.txt |  7 +++++--
 git-pull.sh              | 15 ++++++++++++++-
 t/t5520-pull.sh          | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 3 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 5978d35..4ebd3b5 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1884,8 +1884,11 @@ pretty.<name>::
 pull.mode::
 	When "git pull" is run, this determines if it would either merge or
 	rebase the fetched branch. The possible values are 'merge',
-	'rebase', and 'rebase-preserve'. See "branch.<name>.pullmode" for doing
-	this in a non branch-specific manner.
+	'rebase', 'merge-ff-only,' and 'rebase-preserve'.
+	If 'merge-ff-only' is specified, the merge will only succeed if it's
+	fast-forward.
+	See "branch.<name>.pullmode" for doing this in a non branch-specific
+	manner.
 +
 	When 'rebase-preserve', also pass `--preserve-merges` along to
 	'git rebase' so that locally committed merge commits will not be
diff --git a/git-pull.sh b/git-pull.sh
index 26e4e55..946cbbe 100755
--- a/git-pull.sh
+++ b/git-pull.sh
@@ -57,7 +57,7 @@ then
 	mode=$(git config pull.mode)
 fi
 case "$mode" in
-merge|rebase|'')
+merge|rebase|merge-ff-only|'')
 	;;
 rebase-preserve)
 	mode="rebase"
@@ -83,6 +83,7 @@ then
 		warn "$(gettext "Please use pull.mode and branch.<name>.pullmode instead.")"
 	fi
 fi
+test -z "$mode" && mode=merge
 dry_run=
 while :
 do
@@ -318,6 +319,18 @@ case "$merge_head" in
 		die "$(gettext "Cannot rebase onto multiple branches")"
 	fi
 	;;
+*)
+	# check if a non-fast-foward merge would be needed
+	merge_head=${merge_head% }
+	if test "$mode" = merge-ff-only -a -z "$no_ff$ff_only${squash#--no-squash}" &&
+		test -n "$orig_head" &&
+		! git merge-base --is-ancestor "$orig_head" "$merge_head" &&
+		! git merge-base --is-ancestor "$merge_head" "$orig_head"
+	then
+		die "$(gettext "The pull was not fast-forward, please either merge or rebase.
+If unsure, run 'git pull --merge'.")"
+	fi
+	;;
 esac
 
 # Pulling into unborn branch: a shorthand for branching off
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index 01ad17a..2e2b476 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -365,4 +365,40 @@ test_expect_success 'git pull --rebase against local branch' '
 	test file = "$(cat file2)"
 '
 
+test_expect_success 'git pull fast-forward' '
+	test_when_finished "git checkout master && git branch -D other test" &&
+	test_config pull.mode merge-ff-only &&
+	git checkout -b other master &&
+	>new &&
+	git add new &&
+	git commit -m new &&
+	git checkout -b test -t other &&
+	git reset --hard master &&
+	git pull
+'
+
+test_expect_success 'git pull non-fast-forward' '
+	test_when_finished "git checkout master && git branch -D other test" &&
+	test_config pull.mode merge-ff-only &&
+	git checkout -b other master^ &&
+	>new &&
+	git add new &&
+	git commit -m new &&
+	git checkout -b test -t other &&
+	git reset --hard master &&
+	test_must_fail git pull
+'
+
+test_expect_success 'git pull non-fast-forward (merge)' '
+	test_when_finished "git checkout master && git branch -D other test" &&
+	test_config pull.mode merge-ff-only &&
+	git checkout -b other master^ &&
+	>new &&
+	git add new &&
+	git commit -m new &&
+	git checkout -b test -t other &&
+	git reset --hard master &&
+	git pull --merge
+'
+
 test_done
-- 
1.9.2+fc1.3.gade8541
Previous: Felipe ContrerasNext: Felipe Contreras
Message 12 of 19 in “Reject non-ff pulls by default”
  1. 0/6 Reject non-ff pulls by defaultFelipe Contreras, Apr 29, 2014
  2. 1/6 pull: rename pull.rename to pull.modeFelipe Contreras, Apr 29, 2014
  3. Marat RadchenkoApr 29, 2014
  4. Felipe ContrerasApr 29, 2014
  5. Philip OakleyApr 29, 2014
  6. Felipe ContrerasApr 29, 2014
  7. Philip OakleyApr 29, 2014
  8. Richard HansenApr 30, 2014
  9. 2/6 pull: migrate all the tests to pull.modeFelipe Contreras, Apr 29, 2014
  10. 3/6 pull: refactor $rebase variable into $modeFelipe Contreras, Apr 29, 2014
  11. 4/6 pull: add --merge optionFelipe Contreras, Apr 29, 2014
  12. 5/6 pull: add merge-ff-only optionFelipe Contreras, Apr 29, 2014
  13. 6/6 pull: only allow ff merges by defaultFelipe Contreras, Apr 29, 2014
  14. Junio C HamanoApr 30, 2014
  15. Felipe ContrerasApr 30, 2014
  16. Junio C HamanoApr 30, 2014
  17. Felipe ContrerasApr 30, 2014
  18. Junio C HamanoApr 30, 2014
  19. Junio C HamanoApr 30, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.