71

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

manojlds
  • 290,304
  • 63
  • 469
  • 417
opensas
  • 60,462
  • 79
  • 252
  • 386
  • already checked this question http://stackoverflow.com/questions/7188314/git-directory-and-working-directory – opensas Oct 01 '11 at 20:04

6 Answers6

132

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.

Danny
  • 1,321
  • 1
  • 7
  • 4
15

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

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

Alex.U
  • 1,631
  • 2
  • 16
  • 26
user14827129
  • 167
  • 1
  • 2
7

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.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Lance
  • 2,774
  • 4
  • 37
  • 57
  • 1
    `--git-dir` does more than that. `-C` is a better option. From manual page for Git: `If you just want to run git as if it was started in then use git -C .` – Ruslan Gunawardana Jun 23 '20 at 12:43
  • Hello, I voted several years ago for this post but when I reused this command I realized that it works differently. I use it in a git action my git head was updated however no files were changed I had to run a hard to get the changes in the . I think that dir should be used only if you are in the project folder but the .git is in another folder than this one – Pascal de Sélys Jun 26 '21 at 09:02
4

In my case, this is only working solution:

git --git-dir=/gitbackup/test/.git pull
Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69
  • This does not work as expected. It will pull the changes into the `.git` but it will not overwrite and update the project files, which is the expected behaviour from a `git pull`. You will be left with a `git log` that shows the new commits, but a bunch of apparent uncommitted changes to the project directory, which are the old versions of the files that are diffed against the new commits. – Brettins Sep 19 '22 at 17:22
1

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
Noam Manos
  • 15,216
  • 3
  • 86
  • 85
1

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
ryantm
  • 8,217
  • 6
  • 45
  • 57
  • I tried it, it gives: sas@test:~/calimoucho$ git pull ../apps fatal: Not a git repository (or any of the parent directories): .git (tried also with ../apps/myApp, ../apps/myApp/.git) – opensas Oct 01 '11 at 20:22
  • 1
    ah! that's the problem, standing at the cloned repo folder everything works fine (in fact, a simple 'git pull' works ok)... the problem is how to do it from another folder... – opensas Oct 02 '11 at 13:38
  • Shouldn't the `--work-tree` come *before* the `pull` comand, like this: `git --work-tree=checkout/myApp/ pull ../../apps/myapp` – Potherca Aug 03 '12 at 19:35