executing a git pull from a different directory
I'm configuring calimoucho (a little play continuos integration server), and for it to work I need to run a command to pull a cloned git hub repository from outside it.
to be more precise, I'll explain it with an example.
I have the following repository
cd /home/sas
mkdir apps
cd apps
mkdir myApp
cd myApp
git init
echo "my file" > file
git add .
git commit -m "initial commit"
Just a silly test repository where my app is supossed to be
Now I need to clone that repository to a checkout folder.
cd /home/sas
mkdir calimoucho
cd calimoucho
mkdir checkout
cd checkout
git clone /home/sas/apps/myApp/
so I have the following directory structure
~/apps
myapp
.git
file
~/calimoucho
checkout
myapp
.git
file
The continuos integration server will have to pull new changes from ~/apps/myapp to ~/calimoucho/checkout/myapp, running a command line sentence from ~/calimoucho
I try with the following command
~/calimoucho$ git --git-dir=/ho开发者_开发百科me/sas/apps/myApp/.git --work-tree=/home/sas/calimoucho/checkout/myApp/ pull
and I get the following error
fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.
if I don't specify the --work-tree option, the pull is issued, but changes are applied to ~/calimoucho folder instead of ~/calimoucho/checkout/myApp
any idea how to update the cloned repo from the ~/calimoucho folder?
thanks a lot
I had this question, too. I found the answer in the git documentation (https://git-scm.com/docs/git).
Run the command
git -C <git-working-directory> pull <git remote>
The specific command to answer this question is
git -C checkout/myApp/ pull
Note that it is important -C <git-working-directory>
comes before the pull
command and any additional pull options can be specified at the end of the command. In the example above, the git clone
command would have setup the default remote repository ~/apps/myapp so it is not required to specify the remote repository.
git -C ~/Documents/blar/blar/directory/ pull
This worked for me after hours of searching. I'm on a Mac in Terminal.
This one worked for me:
git --git-dir=/home/myuser/awesomeproject/.git --work-tree=/home/myuser/awesomeproject pull
I'm using it inside a post-update hook to automatically pull and restart pm2 on my test server.
In my case, this is only working solution:
git --git-dir=/gitbackup/test/.git pull
You should not set the work-tree to a different repository than the git-dir variable. I think they are meant to be used when you don't want the .git folder to be in the same directory as your working tree. Instead try this:
~/calimoucho/$ git pull --work-tree=checkout/myApp/ ../../apps/myapp
In case your git version doesn't like the git-working-directory option:
Unknown option: -C
Then use the push-directory command beforehand.
For example, this will pull your git project from parent directory (or clone if it doesn't exists):
pushd ./project-name && git pull && popd || git clone https://repo-url/project-name
精彩评论