Using `git --git-dir /path/to/git pull /path/to/other/bare-git master` doesn't automatically update the working tree. Why not?

后端 未结 1 1049
梦如初夏
梦如初夏 2021-01-13 00:02

I found a peculiar behavior with git, and I can reproduce it every time on my machine.

If I have two local repositories, one bare inside the folder express.git

相关标签:
1条回答
  • 2021-01-13 00:36

    If you run git --git-dir=some/dir/.git pull, by default git will assume the current directory is the work tree. Not the parent of some/dir/.git, but your current pwd. This means that running that command will try to update the current directory as if it's the work tree and will end up writing files into your pwd that don't belong there.

    The appropriate solution is to use the --work-tree flag in conjunction with --git-dir to tell it where the work tree is. In this case you'd want git --git-dir=some/dir/.git --work-tree=some/dir pull. However, after experimentation it seems there's a second problem here. If you try this command as-is, you'll probably be told git-pull cannot be used without a working tree. It seems the issue here is git needs its work tree to be an absolute path instead of a relative one.

    What you really want to run is git --git-dir=some/dir/.git --work-tree="$PWD"/some/dir pull. Alternatively, you could just try cd some/dir && git pull. If you don't want to change your cwd, you can wrap this in a subshell, i.e. ( cd some/dir && git pull ).

    0 讨论(0)
提交回复
热议问题