executing a git pull from a different directory

GitGit Pull

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

git -C ~/Documents/blar/blar/directory/ pull

This worked for me after hours of searching. I'm on a Mac in Terminal.

Solution 3 - Git

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.

Solution 4 - Git

In my case, this is only working solution:

git --git-dir=/gitbackup/test/.git pull

Solution 5 - Git

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

Solution 6 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionopensasView Question on Stackoverflow
Solution 1 - GitDannyView Answer on Stackoverflow
Solution 2 - Gituser14827129View Answer on Stackoverflow
Solution 3 - GitLanceView Answer on Stackoverflow
Solution 4 - GitErçin DedeoğluView Answer on Stackoverflow
Solution 5 - GitryantmView Answer on Stackoverflow
Solution 6 - GitNoam ManosView Answer on Stackoverflow