Let's say I have a directory, /X/Y
, which is a git repository. Is it possible to somehow call a command like git pull
from inside /X
, but targeting the /X/Y
directory?
EDIT: I guess I was wondering specifically: is it possible to do this using the a git command, but without having to change directories?
NOTE: I've accepted VonC's answer as it's much more elegant than previous options. For people running Git older than 1.8.5, please see bstpierre's answer below.
This question is related to
git
For anyone like me that was trying to do this via a drush (Drupal shell) command on a remote server, you will not be able to use the solution that requires you to CD into the working directory:
Instead you need to use the solution that breaks up the pull into a fetch & merge:
drush @remote exec git --git-dir=/REPO/PATH --work-tree=/REPO/WORKDIR-PATH fetch origin
drush @remote exec git --git-dir=/REPO/PATH --work-tree=/REPO/WORKDIR-PATH merge origin/branch
As some of my servers are on an old Ubuntu LTS versions, I can't easily upgrade git to the latest version (which supports the -C option as described in some answers).
This trick works well for me, especially because it does not have the side effect of some other answers that leave you in a different directory from where you started.
pushd /X/Y
git pull
popd
Or, doing it as a one-liner:
pushd /X/Y; git pull; popd
Both Linux and Windows have pushd and popd commands.
You may wrap it in a bash script or git alias:
cd /X/Y && git pull && cd -
This might be a similar problem, but you can also simply chain you commands. eg
On one line
cd ~/Sites/yourdir/web;git pull origin master
Or via SSH.
ssh [email protected] -t "cd ~/Sites/thedir/web;git pull origin master"
This post is a bit old so could be there was a bug andit was fixed, but I just did this:
git --work-tree=/X/Y --git-dir=/X/Y/.git pull origin branch
And it worked. Took me a minute to figure out that it wanted the dotfile and the parent directory (in a standard setup those are always parent/child but not in ALL setups, so they need to be specified explicitly.
Using combination pushd
, git pull
and popd
, we can achieve this functionality:
pushd <path-to-git-repo> && git pull && popd
For example:
pushd "E:\Fake Directory\gitrepo" && git pull && popd
You can write a script like this:
cd /X/Y
git pull
You can name it something like gitpull
.
If you'd rather have it do arbitrary directories instead of /X/Y
:
cd $1
git pull
Then you can call it with gitpull /X/Z
Lastly, you can try finding repositories. I have a ~/git
folder which contains repositories, and you can use this to do a pull on all of them.
g=`find /X -name .git`
for repo in ${g[@]}
do
cd ${repo}
cd ..
git pull
done
Edit:
There's either a bug with git pull
, or you can't do what you're trying to do with that command. You can however, do it with fetch and merge:
cd /X
git --git-dir=/X/Y/.git fetch
git --git-dir=/X/Y/.git --work-tree=/X/Y merge origin/master
Original answer:
Assuming you're running bash or similar, you can do (cd /X/Y; git pull)
.
The git man page specifies some variables (see "The git Repository") that seem like they should help, but I can't make them work right (with my repository in /tmp/ggg2):
GIT_WORK_TREE=/tmp/ggg2 GIT_DIR=/tmp/ggg2/.git git pull
fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.
Running the command below while my cwd is /tmp updates that repo, but the updated file appears in /tmp instead of the working tree /tmp/ggg2:
GIT_DIR=/tmp/ggg2/.git git pull
See also this answer to a similar question, which demonstrates the --git-dir
and --work-tree
flags.
Source: Stackoverflow.com