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

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

If I have two local repositories, one of which is inside the express.git folder, and the other is not bare with a working tree inside the express folder, as in one parent folder, I can run the git pull ../express.git from inside express folders. This automatically updates everything inside express .

However, if I run the git --git-dir /home/cisw470p/stu006/express/.git pull /home/cisw470p/stu006/express.git master command git --git-dir /home/cisw470p/stu006/express/.git pull /home/cisw470p/stu006/express.git master from a location not located in the git repository, then the express repository will cause changes but will not automatically update the working tree. Then I have to run git add . to add all the changes, and then make another commit inside express , and now everything is fine.

Why is not the long version of the command with the parameter - git-dir not automatically update the working tree for express ? Is there a reason for this, or have I found an error?

EDIT: I just tried again, but edited another file, and now it worked. I am completely lost.

+6
source share
1 answer

If you run git --git-dir=some/dir/.git pull , by default git will consider the current directory as a working tree. Not the parent element of some/dir/.git , but your current pwd . This means that the execution of this command will try to update the current directory as if it were a working tree, and finish writing files to your pwd , which does not belong there.

The appropriate solution is to use the --work-tree flag in conjunction with --git-dir to indicate where this work tree is. In this case, you need git --git-dir=some/dir/.git --work-tree=some/dir pull . However, after the experiments, a second problem arises here. If you try this command as is, you will probably be told git-pull cannot be used without a working tree . The problem seems to be git here, requiring its work tree to be an absolute path, not a relative one.

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

+10
source

Source: https://habr.com/ru/post/910952/


All Articles