Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are git commands supposed to be run under the working directory directly?

Tags:

git

linux

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 commands which can take an argument which specifies some files, e.g. git add, and
  • git commands which doesn't take an argument that specifies some files, e.g. git pull, git push.
like image 783
Tim Avatar asked Feb 09 '23 05:02

Tim


1 Answers

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)

like image 149
VonC Avatar answered Feb 13 '23 02:02

VonC