Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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, and the other non-bare with a working tree inside the folder express, both in the same parent folder, I can do the command git pull ../express.git from inside the express folder. This automatically updates everything inside express.

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

Why doesn't the long version of the command using the --git-dir option not automatically update the working tree for express? Is there a reason for this, or did I find a bug?

EDIT: I just tried it again but edited a different file and now it worked. I'm completely lost.

like image 952
trusktr Avatar asked Mar 16 '12 22:03

trusktr


1 Answers

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 ).

like image 124
Lily Ballard Avatar answered Sep 17 '22 22:09

Lily Ballard