Suppose I have a git working directory, i.e. the directory which has a subdirectory called .git
.
I wonder if the current directory matters when I run a git command. Is it okay to run a git command
directly under the working directory
directly under some subdirectory of (subdirectory of) the working directory
directly under the parent directory of the working directory?
Consider
git add
, and git pull
, git push
.directly under the parent directory of the working directory?
Actually you can run it anywhere you want as long as you reference the git repo:
git --git-dir=/path/to/my/repo/.git add .
That means wherever you are (.: current folder) will be considered as your working tree. A
You can even specify your working tree:
git --work-tree=/a/path --git-dir=/path/to/my/repo/.git add .
In that latter case, you even can execute that last command anywhere you want. The '.
' will be the work-tree /a/path
.
Since git 1.8.5, you also have the -C
option:
git -C /path/to/my/repo add .
Again, you can execute it anywhere you want, but the command will internally do a cd /path/to/my/repo
first, and then execute the add .
. That means the '.
' will actually be /path/to/my/repo
.
Finally, since git 2.5, a git repo supports multiple working trees, so you may execute your command in a folder which does not include a subfolder .git
(but actually a kind of symbolic link to /path/to/my/repo/git
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With