13

With svn, I was able to run commands on files in a checkout, without having cd into that checkout first. For example:

# Located in /tmp, running svn operation on /home/d5ve/checkout
d5ve@host:/tmp> svn add /home/d5ve/checkout/myfile.txt
d5ve@host:/tmp> svn diff /home/d5ve/checkout/myfile.txt
d5ve@host:/tmp> svn commit /home/d5ve/checkout/myfile.txt

When I attempt this workflow using git, I get an error:

# Located in /tmp, attempting git operation on /home/d5ve/checkout2
d5ve@host:/tmp> git add /home/d5ve/checkout2/myfile.txt
fatal: Not a git repository (or any of the parent directories): .git

I've tried using the --git-dir and --work-tree flags, but that also seemed to fail.

Any suggestions git people? I use this workflow a lot and really miss it when using git.

UPDATE 2016 The current correct answer to this is to use the -C flag to git, which was introduced in version 1.8.5 in 2013. See https://stackoverflow.com/a/35899275/357336

UPDATE: Based upon lunaryorn answer below, I've created a simple perl script which works out the location of the .git directory from the file paths, and sets the GIT_WORK_TREE and GIT_DIR environmental variables for the command.

Please have a look at: http://github.com/d5ve/rgit

USAGE: rgit COMMAND [ARGS]

Basically just replace git with rgit in a command, and you can run the commands from outside the repository.

cd /tmp    
rgit diff /home/d5ve/checkout1
rgit add /home/d5ve/checkout1/rgit.pl
rgit commit /home/d5ve/checkout1/
d5ve
  • 1,168
  • 10
  • 18
  • Possible duplicate of [How do I execute a Git command without being on the repository folder?](http://stackoverflow.com/questions/7149984/how-do-i-execute-a-git-command-without-being-on-the-repository-folder) – Philipp Wendler Feb 14 '17 at 08:23

2 Answers2

16

You have to use both options together:

git --git-dir=/home/d5ve/checkout2/.git --work-tree=/home/d5ve/checkout2/ add /home/d5ve/checkout2/myfile.txt

Note, that --git-dir does not refer to the directory containing working copy, but to the .git subdirectory, which contains the repository itself. git provides two environment variables to set these options permanently for a session:

export GIT_WORK_TREE="/home/d5ve/checkout2"
export GIT_DIR="${GIT_WORK_TREE}/.git"
git status
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    Cheers lunaryorn, that's combination I didn't try. I have a bunch of different repositories, so I'll try to put together a bash script which gleans the GIT_WORK_TREE and GIT_DIR from the directory mentioned in the command and sets them for the git command itself. Something like `mygit add /home/d5ve/checkout2/myfile.txt` – d5ve Jul 09 '10 at 09:42
1

Please check this slightly different question which has an answer working perfectly, different to the workaround proposed in the other answer here.

How do I execute a Git command without being on the repository folder?

According to some moderator I am not allowed to post the answer here (some kind of Chuck Norris fan I guess).

Community
  • 1
  • 1
calandoa
  • 5,668
  • 2
  • 28
  • 25
  • As linked here, the -C option to git is now the correct way to answer this question. This flag was added in git v1.8.5 in 2013. Cheers calandoa. – d5ve Apr 06 '16 at 04:26