4

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.
Tim
  • 1
  • 141
  • 372
  • 590
  • If you wanted a **really** comprehensive answer, it would need to cover behavior as modified by environment variables (`GIT_DIR`, `GIT_CEILING_DIRECTORIES`, etc). – Charles Duffy Dec 27 '15 at 01:19
  • You can also use `git -C` *`dir`* `...` which effectively changes to the specified directory before running the command. – Keith Thompson Dec 27 '15 at 03:13

3 Answers3

5

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)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. in `git --git-dir=/path/to/my/repo/.git add .`, what does the argument `.` for `add` refer to? The directory where I run the git command, or `/path/to/my/repo/.git`? – Tim Dec 27 '15 at 23:09
  • @Tim the directory where you run the command. It is a good trick when you want to import a content which is not in your git repo. – VonC Dec 28 '15 at 07:44
  • Does your comment apply to `git --work-tree=/a/path --git-dir=/path/to/my/repo/.git add .` and `git -C /path/to/my/repo add .`? So does shell's file expansion always happen before git does `cd /path/to/my/repo`? – Tim Dec 28 '15 at 14:25
  • @Tim No: The '`.`' in the former refers the work tree `/a/path`. The '`.`' in the latter refers to `/path/to/my/repo`. I have edited the answer to make it clearer: see the latest revision: http://stackoverflow.com/posts/34478315/revisions – VonC Dec 28 '15 at 16:26
  • @V: Shouldn't bash's file expansion happen before the git command runs (e.g. to change current directory to `/path/to/my/repo`)? Then git command knows an argument which is the directory from where I run it, instead of `.`. – Tim Dec 28 '15 at 18:13
  • @Tim not in that case: all the details are in http://stackoverflow.com/a/20115526/6309 – VonC Dec 28 '15 at 18:48
2

It's OK to run both type of commands in both

directly under the working directory
directly under some subdirectory of (subdirectory of...) the working directory

Note, that you should use paths relative to directory where you are

You can't (by default) run any git command in parent directory. You'll get a message that you aren't in any repo.

RiaD
  • 46,822
  • 11
  • 79
  • 123
2

I wonder if the current directory matters when I run a git command.

It does. git searches the current working directory for the .git subdirectory, and if it doesn't find it then it searches the parent directory, and so on until it finds it.

  • directly under the working directory

Yes.

  • directly under some subdirectory of (subdirectory of) the working directory

Yes.

  • directly under the parent directory of the working directory?

No, not by default. It is possible to tell git where to find the .git directory so that it doesn't search, but this is not the usual mode of operation.

Documentation on the relevant environment variables:

GIT_DIR is the location of the .git folder. If this isn’t specified, Git walks up the directory tree until it gets to ~ or /, looking for a .git directory at every step.

GIT_WORK_TREE is the location of the root of the working directory for a non-bare repository. If not specified, the parent directory of $GIT_DIR is used.

Source

git commands which can take an argument which specifies some files, e.g. git add, and

These commands need to know both where the .git directory is and the relative position of paths in the work tree.

git commands which doesn't take an argument that specifies some files, e.g. git pull, git push.

Commands like git push or git fetch need to know where the .git directory is, but don't care about the work tree. git pull does since it does a git merge which modifies files in the working tree.

Community
  • 1
  • 1
bames53
  • 86,085
  • 15
  • 179
  • 244