title: Can I perform git operations (add/diff/commit) on files in a repository when my current directory isn't in the repository
body: <p>With svn, I was able to run commands on files in a checkout, without having <code>cd</code> into that checkout first. For example:</p>
  
  <pre><code># Located in /tmp, running svn operation on /home/d5ve/checkout
  d5ve@host:/tmp&gt; svn add /home/d5ve/checkout/myfile.txt
  d5ve@host:/tmp&gt; svn diff /home/d5ve/checkout/myfile.txt
  d5ve@host:/tmp&gt; svn commit /home/d5ve/checkout/myfile.txt
  </code></pre>
  
  <p>When I attempt this workflow using git, I get an error:</p>
  
  <pre><code># Located in /tmp, attempting git operation on /home/d5ve/checkout2
  d5ve@host:/tmp&gt; git add /home/d5ve/checkout2/myfile.txt
  fatal: Not a git repository (or any of the parent directories): .git
  </code></pre>
  
  <p>I've tried using the <code>--git-dir</code> and <code>--work-tree</code> flags, but that also seemed to fail. </p>
  
  <p>Any suggestions git people? I use this workflow a lot and really miss it when using git.</p>
  
  <p><strong>UPDATE 2016</strong> The current correct answer to this is to use the <code>-C</code> flag to git, which was introduced in version 1.8.5 in 2013. See <a href="https://stackoverflow.com/a/35899275/357336">https://stackoverflow.com/a/35899275/357336</a></p>
  
  <p><strong>UPDATE:</strong> 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.</p>
  
  <p>Please have a look at: <a href="http://github.com/d5ve/rgit" rel="nofollow noreferrer">http://github.com/d5ve/rgit</a></p>
  
  <p>USAGE: rgit COMMAND [ARGS]</p>
  
  <p>Basically just replace <em>git</em> with <em>rgit</em> in a command, and you can run the commands from outside the repository.</p>
  
  <pre><code>cd /tmp    
  rgit diff /home/d5ve/checkout1
  rgit add /home/d5ve/checkout1/rgit.pl
  rgit commit /home/d5ve/checkout1/
  </code></pre>