Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use git from another directory?

Tags:

git

Assume there is folder structure like:

repos
    /repo1  <-- here is git repository

I do:

cd repos

And how can I now use repository in /repo1 still being in repos directory? I don't want to do

cd repo1
git status (...)
git commit (...)
...

but something like:

git --git-dir=repo1 (...)

or

git --work-tree=repo1 (...)

I want to do ALL git commands in that style, event git init. What's the correct approach?

like image 217
bartek Avatar asked Feb 18 '23 11:02

bartek


1 Answers

Git has the -C <path> option (like tar -C <path>) that changes the working directory for Git to path and then executes the command in that directory. Example run:

$ mkdir gitrepo
$ git -C gitrepo init
Initialized empty Git repository in /home/foo/gitrepo/.git/

man git:

-C <path>

Run as if git was started in <path> instead of the current working directory. When multiple -C options are given, each subsequent non-absolute -C <path> is interpreted relative to the preceding -C <path>.

This option affects options that expect path name like --git-dir and --work-tree in that their interpretations of the path names would be made relative to the working directory caused by the -C option. For example the following invocations are equivalent:

git --git-dir=a.git --work-tree=b -C c status
git --git-dir=c/a.git --work-tree=c/b status

My version of Git is 2.16.1.

like image 144
Stefan van den Akker Avatar answered Feb 27 '23 10:02

Stefan van den Akker