DEV Community

Heba Waly
Heba Waly

Posted on • Originally published at Medium

Run git from any directory: git -C Vs --git-dir

The more I dig into Git the more I come across some cool features that I’ve never used before although I’ve been using Git for years.

One of those cool features is being able to run Git from any directory (not necessarily the repo’s directory) by sending some extra parameters to the git command.

git -C <path> <command>

Runs the command as if git was started in <path> instead of the current working directory.

For example, if I’m working on a certain project, and then I decided to check the status of another project really quick, I can either cd to the other project, run git status and then cd back to the previous project and continue my work.

Or I can do it all in one command using git -C path/to/other/repo status

git --git-dir=<path> <command>

Another way you can accomplish the same thing as git -C is by using —-git-dir. Git directory is the .dir folder that Git adds to your repo.

For example, git --git-dir=myrepo/.git status But be careful if you decided to use this parameter, you’ll need to set the work tree as well to make sure that Git will run your command across the correct work tree, like this:

git --git-dir=myrepo/.git --work-tree=myrepo status

What is a work tree?

The difference between git work tree and git repository can be a bit confusing at first, think of the work tree as a tree (graph) that’s tracking every single change you make in your repo. A Git repo can have multiple work trees, which is another cool feature you can read more about here.

Given that both git -C and git --git-dir can help you accomplish the same thing, which one should you go with? git -C is the answer. It was added to git years after --git-dir to make it easier for you to run git from another working directory, and as you can tell it’s much easier to use ;)

Oldest comments (0)