41

How would I run git commands on a repo when I have not cd'd into that directory?

I.e. I want to run git branch /repos/myrepo.git

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
Mike007
  • 530
  • 1
  • 5
  • 10
  • Possible duplicate of [git pull while not in a git directory](http://stackoverflow.com/questions/5083224/git-pull-while-not-in-a-git-directory) – Trevor Boyd Smith Mar 22 '17 at 18:08

2 Answers2

56

Starting with git 1.8.5, use the -C option.

git -C "/Users/michael/Development/Projects/opensource/dvsc-backup" status

Otherwise, you have to specify --work-tree as well as --git-dir

git --work-tree="/Users/michael/Development/Projects/opensource/dvsc-backup" --git-dir="/Users/michael/Development/Projects/opensource/dvsc-backup/.git" status
Christian Long
  • 10,385
  • 6
  • 60
  • 58
34

--git-dir=<path>

Set the path to the repository. This can also be controlled by setting the GIT_DIR environment variable. It can be an absolute path or relative path to current working directory.

http://www.kernel.org/pub/software/scm/git/docs/git.html

Note that <path> above means the path to the actual git directory (project_dir/.git) not just the project directory (project_dir).

Tyler
  • 21,762
  • 11
  • 61
  • 90
  • I have tried this and it does not work for me.
      git --git-dir=/Users/michael/Development/Projects/opensource/dvsc-backup/  
    – Mike007 Aug 14 '11 at 23:32
  • Not a git repository. It is one and everything works normally when I cd into the dir and run git commands on it. – Mike007 Aug 14 '11 at 23:38
  • 3
    Maybe `git --git-dir=/Users/michael/Development/Projects/opensource/dvsc-backup/.git`? – Tyler Aug 14 '11 at 23:41
  • 3
    You must add the .git folder to the end of the --git-dir= for this to work. Does not work: git --git-dir=/Users/michael/Development/Projects/opensource/dvsc-backup/ Works: git --git-dir=/Users/michael/Development/Projects/opensource/dvsc-backup/.git/ branch – Mike007 Aug 14 '11 at 23:44
  • If it's a bare repository you can just use the folder that you've cloned into (there is no .git folder anyway) – KayEss Oct 08 '13 at 06:51