Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Last active April 28, 2024 01:52
Show Gist options
  • Save jeffkreeftmeijer/00734a60ad6c38ba798239aebb9c6142 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/00734a60ad6c38ba798239aebb9c6142 to your computer and use it in GitHub Desktop.

Run Git commands from outside a repository directory

There are multiple options to run a Git command from outside of a repository directory[fn:todo]. From least to most flexible, these are the --git-dir -C and --work-tree options.

--git-dir

Pass the --git-dir option pointing to the .git directory in the repository:

git --git-dir=/tmp/repository/.git log --format=oneline

#+RESULTS[19aeaafbe67db13d3cc31eec8930c3355b347c32]:

6b5a4d9ed5346ec93c880c73eac43012eb9a809b Add sub/file.txt
37535441d4b3517a2362cde777af3d697acf5024 Add file.txt

-C

Much like changing directories[fn:cd-equivalent], the -C option runs the command as if if was run from within the repository.

git -C /tmp/repository/ log --format=oneline

#+RESULTS[59ed63e10fe7c67050fc2704eebc9368e1dca477]:

13b6797244a57111f681ea16ade4b73196b1d68a Add sub/file.txt
6e3cb392609cb23e2185f3711cceb65f7e0ff085 Add file.txt

It can also point to a file or sub directory somewhere in the repository:

git -C /tmp/repository/sub log --format=oneline

#+RESULTS[5fa40804a1d8700b593147c7de186e3ec5284e05]:

669bf20cbb1103d9935ca8afd4a1a3678bd1de8d Add sub/file.txt
0a4af8f4f62b7c3d2c087aae565c86fcc6e95814 Add file.txt

--work-tree

The --work-tree option behaves like ==–git-dir=, but links to a directory in the working tree.

git --work-tree=/tmp/repository/ log --format=oneline

#+RESULTS[402ae838c7b377f3aa7acd84fc705e43260305d2]:

1cdfc73ffdf6601a77ace6bc2c9bda8f389902cd Add sub/file.txt
0a8fbe8f63f672f08509f36771d67d750bd322f9 Add file.txt

It can also point to a file or sub directory somewhere in the repository:

git --work-tree=/tmp/repository/sub log --format=oneline

#+RESULTS[c7e0d552fb65e998095a94dfb068eb77bfe370b9]:

fd3d2da957b7443dbe88fd243544a14d07369e3c Add sub/file.txt
77eab2288dba005018c9877bb404633f327beed9 Add file.txt
git --work-tree=/tmp/repository/sub/file.txt log --format=oneline

#+RESULTS[6381fbc87bf0c4ed1fd578ad25becd4c7a950933]:

65efe2ec22836b020019ef8b5461a81de9e6a6f7 Add sub/file.txt
a1bec894dfd2ed9b2df9970bff6fc9111234c652 Add file.txt

You can even link to files that don’t exist, provided their containing directories do:

git --work-tree=/tmp/repository/does-not-exist.txt log --format=oneline

#+RESULTS[fb28fc86838fec8031b86c21eab8f5045db28ae7]:

09581359f16a9e7ee0ecdc7121978ec513766e30 Add sub/file.txt
75206992e4ce68d5b23c70720290d4126a465d5c Add file.txt

[fn:todo] These are useful for when you need to get information about a repository you’re not currently in, but also for learning information about a file you don’t know the containing repository of. With -C, for example, you can list the commits for a file without first knowing the repository location:

git --work-tree=/tmp/repository/sub log /tmp/repository/sub/file.txt

[fn:cd-equivalent] In fact, using the -C option is equivalent to switching directories in a subshell:

(cd /tmp/repository && git log --format=oneline)

#+RESULTS[50b02a4907907f1376fcd4d19aed552042082a42]:

01025361f1d1f39a550442642ffa0e1d32bc05ab Add sub/file.txt
2d30004818ec1c54b0c11a7c3b0185af043ce5c2 Add file.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment