executing a git pull from a different directory

opensas picture opensas · Oct 1, 2011 · Viewed 23.5k times · Source

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=/home/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

Answer

Danny picture Danny · Feb 1, 2018

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.