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

Re: [Bug] git pull doesn't recognize --work-tree parameter

From
Jeff King <peff@peff.net>
Date
Oct 13, 2011, 15:59 UTC
Message-ID
<20111013155923.GA13134@sigill.intra.peff.net>
In-Reply-To
<E95C75ED-99F2-463C-A1AB-0F8152696739@jetbrains.com>
On Thu, Oct 13, 2011 at 12:38:53PM +0400, Kirill Likhodedov wrote:
Show 11 quoted lines
> 'git pull' doesn't work from outside the working tree even if '--work-tree' is specified:
> 
> # git version
> git version 1.7.6
> # git --git-dir=/Users/loki/sandbox/git/child/.git --work-tree=/Users/loki/sandbox/git/child pull
> fatal: /opt/local/libexec/git-core/git-pull cannot be used without a working tree.
> 
> Note that  'git fetch' and 'git merge origin/master' work fine, so 'git pull' should be easy to fix :)
> 
> # git --git-dir=/Users/loki/sandbox/git/child/.git --work-tree=/Users/loki/sandbox/git/child merge origin/master
> Already up-to-date.

This is a known issue, and the fix is a one-liner, but it needed somebody to look through the pull script to make sure it wasn't introducing any new bugs. I just did this; the patch below should fix your problem.

-- >8 --
Subject: [PATCH] pull,rebase: handle GIT_WORK_TREE better

You can't currently run git-pull or git-rebase from outside of the work tree, even with GIT_WORK_TREE set, due to an overeager require_work_tree function. Commit e2eb527 documents this problem and provides the infrastructure for a fix, but left it to later commits to audit and update individual scripts.

Changing these scripts to use require_work_tree_exists is easy to verify. We immediately call cd_to_toplevel, anyway. Therefore no matter which function we use, the state afterwards is one of:

  1. We have a work tree, and we are at the top level.
  2. We don't have a work tree, and we have died.

The only catch is that we must also make sure no code that ran before the cd_to_toplevel assumed that we were already in the working tree.

In this case, we will only have included shell libraries and called set_reflog_action, neither of which care about the current working directory at all.

Signed-off-by: Jeff King <peff@peff.net>
---
This is the low-hanging, obviously correct fruit. git-am and
git-stash also immediately cd_to_toplevel, but they look at
$PWD or `git rev-parse --show-prefix` beforehand, which
means those uses have to be audited separately.
 git-pull.sh   |    2 +-
 git-rebase.sh |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/git-pull.sh b/git-pull.sh
index 63da37b..902fc4a 100755
--- a/git-pull.sh
+++ b/git-pull.sh
@@ -11,7 +11,7 @@ OPTIONS_SPEC=
 . git-sh-setup
 . git-sh-i18n
 set_reflog_action "pull${1+ $*}"
-require_work_tree
+require_work_tree_exists
 cd_to_toplevel
 
 
diff --git a/git-rebase.sh b/git-rebase.sh
index 6759702..00ca7b9 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -63,7 +63,7 @@ skip!              skip current patch and continue
 "
 . git-sh-setup
 set_reflog_action rebase
-require_work_tree
+require_work_tree_exists
 cd_to_toplevel
 
 LF='
-- 
1.7.6.4.37.g43b58b
Previous: Nguyen Thai Ngoc DuyNext: Junio C Hamano
Message 3 of 10 in “[Bug] git pull doesn't recognize --work-tree parameter”
  1. Kirill LikhodedovOct 13, 2011
  2. Nguyen Thai Ngoc DuyOct 13, 2011
  3. Jeff KingOct 13, 2011
  4. Junio C HamanoOct 13, 2011
  5. Jeff KingOct 13, 2011
  6. Junio C HamanoOct 13, 2011
  7. Jeff KingOct 13, 2011
  8. Jeff KingOct 13, 2011
  9. Junio C HamanoOct 18, 2011
  10. Junio C HamanoOct 13, 2011

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.