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

BUG: git_work_tree issue with git alias and scripting

From
TTTuomo Tanskanen <tuomo@tanskanen.org>
Date
Sep 18, 2015, 08:09 UTC
Message-ID
<CAE_UwRqMnmgDEUjcuxNDXgx_D3MTFxNRTgA9xGB1OvK6_d-bMw@mail.gmail.com>
Hello,

Attached is a simple script exposing a possible bug when git alias is combined with git commands in script. It should be pretty self-explanatory. Check what it does and execute it. On my Linux and OSX, both with git 2.5.1 behave the same, ie. git add fails even the file "test" is in $PWD.

Summary: $GIT_WORK_TREE persists in env, and then some git commands operate
on repo in $PWD (like git log), while some others (like git add) operate on
repo in $GIT_WORK_TREE, making it inconsistent and results in very
unexpected results.
Cheers, Tuomo

--- #!/bin/sh

# setup a test repo mkdir bar cd bar git init echo "test" > bar git add bar git commit -m "test commit in bar" echo "adding this will fail later on" > test cd ..

# setup a repo with an alias and script alias will execute mkdir foo cd foo git init

cat >foo.sh <<EOF #!/bin/sh -x cd $PWD/../bar env | egrep "^(GIT_WORK_TREE|PWD)" echo "Git log works (bar log shows up):" git log --oneline | head -1 echo "Git add does not (tries to add files in GIT_WORK_TREE=foo, while PWD=bar):" git add test EOF chmod 755 foo.sh

cat >>.git/config <<EOF
[alias]
  foo = !$PWD/foo.sh
EOF

git add foo.sh git commit -m "committing foo.sh"

# run test via alias git foo

Message 1 of 1 in “BUG: git_work_tree issue with git alias and scripting”
  1. Tuomo TanskanenSep 18, 2015

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.