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

Re: Pull without fetch

From
DRDamien Robert <damien.olivier.robert@gmail.com>
Date
Apr 8, 2019, 16:11 UTC
Message-ID
<20190408161134.gdxmxvqitxioeibp@feanor>
In-Reply-To
<20190408145340.3rdprg3yu5shpelu@feanor>
From Damien Robert, Mon 08 Apr 2019 at 16:53:40 (+0200) :
> > Others may have a better idea, but I do not immediately see any
> > solution better than inventing a new option to "git pull".

So here is a RFC patch that implements --no-fetch. (I am not sure about the wording of the documentation, and what's the best way to test that fetch was not called).

-- >8 --
From: Damien Robert <damien.olivier.robert+git@gmail.com>
Date: Mon, 8 Apr 2019 17:51:51 +0200
Subject: [PATCH 1/1] pull: add --no-fetch

A common workflow is to do a fetch, review the changes, and then integrate the changes via an explicit merge or rebase.

In the good old days `git pull` was essentially `git fetch` followed by `git merge FETCH_HEAD` so it was easy to separate the fetching part from the integrating part.

But nowadays there are no easy way to do the integrating part of `git pull`. Indeed, `git pull` learnt to read the config values of `branch.<current>.rebase` and `pull.rebase`, whose possible values are growing. At the time of this commit, they can be 'true', 'merges', 'preserve' and 'interactive'.

To remedy this, add a new --no-fetch option to `git pull`.
Signed-off-by: Damien Robert <damien.olivier.robert+git@gmail.com>
---
 Documentation/git-pull.txt |  9 +++++++++
 builtin/pull.c             |  8 ++++++--
 t/t5520-pull.sh            | 14 ++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-pull.txt b/Documentation/git-pull.txt
index 118d9d86f7..cec06bf6e3 100644
--- a/Documentation/git-pull.txt
+++ b/Documentation/git-pull.txt
@@ -144,6 +144,15 @@ This option is only valid when "--rebase" is used.
 Options related to fetching
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+--fetch::
+--no-fetch::
+	Perform a fetch first. This option can be used to override
+	--no-fetch.
++
+With --no-fetch don't fetch before updating the branch via a merge or a
+rebase. This can be used to review changes by doing a fetch first before
+pulling.
+
 include::fetch-options.txt[]
 
 include::pull-fetch-param.txt[]
diff --git a/builtin/pull.c b/builtin/pull.c
index 33db889955..0c14701abe 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -111,6 +111,7 @@ static char *opt_gpg_sign;
 static int opt_allow_unrelated_histories;
 
 /* Options passed to git-fetch */
+static int opt_fetch = 1;
 static char *opt_all;
 static char *opt_append;
 static char *opt_upload_pack;
@@ -195,6 +196,8 @@ static struct option pull_options[] = {
 
 	/* Options passed to git-fetch */
 	OPT_GROUP(N_("Options related to fetching")),
+	OPT_BOOL(0, "fetch", &opt_fetch,
+		N_("fetch before merging / rebasing (default)")),
 	OPT_PASSTHRU(0, "all", &opt_all, NULL,
 		N_("fetch from all remotes"),
 		PARSE_OPT_NOARG),
@@ -912,8 +915,9 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
 			oidclr(&rebase_fork_point);
 	}
 
-	if (run_fetch(repo, refspecs))
-		return 1;
+	if (opt_fetch)
+		if (run_fetch(repo, refspecs))
+			return 1;
 
 	if (opt_dry_run)
 		return 0;
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index cf4cc32fd0..058c85e6a4 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -134,6 +134,20 @@ test_expect_success 'the default remote . should not break explicit pull' '
 	test_cmp reflog.expected reflog.fuzzy
 '
 
+test_expect_success 'With --no-fetch will not fetch but still merge pending changes' '
+	git checkout -b nofetch master^ &&
+	echo modified >file &&
+	git commit -a -m modified &&
+	git checkout copy &&
+	git reset --hard HEAD^ &&
+	test "$(cat file)" = file &&
+	stat --format %Y .git/FETCH_HEAD > fetch_head_before &&
+	git pull --no-fetch . nofetch &&
+	stat --format %Y .git/FETCH_HEAD > fetch_head_after &&
+	test "$(cat file)" = modified &&
+	test_cmp fetch_head_before fetch_head_after
+'
+
 test_expect_success 'fail if wildcard spec does not match any refs' '
 	git checkout -b test copy^ &&
 	test_when_finished "git checkout -f copy && git branch -D test" &&
-- 
Patched on top of v2.21.0-196-g041f5ea1cf (git version 2.21.0)
Previous: Damien RobertNext: Junio C Hamano
Message 7 of 9 in “Pull without fetch”
  1. Damien RobertApr 6, 2019
  2. Junio C HamanoApr 8, 2019
  3. Duy NguyenApr 8, 2019
  4. Ævar Arnfjörð BjarmasonApr 8, 2019
  5. Duy NguyenApr 8, 2019
  6. Damien RobertApr 8, 2019
  7. Damien RobertApr 8, 2019
  8. Junio C HamanoApr 9, 2019
  9. Damien RobertApr 11, 2019

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.