如何从另一个目录使用git?

bar*_*tek 10 git

假设有如下文件夹结构:

repos
    /repo1  <-- here is git repository
Run Code Online (Sandbox Code Playgroud)

我愿意:

cd repos
Run Code Online (Sandbox Code Playgroud)

现在我如何在目录中使用存储/repo1repos?我不想做

cd repo1
git status (...)
git commit (...)
...
Run Code Online (Sandbox Code Playgroud)

但类似的东西:

git --git-dir=repo1 (...)
Run Code Online (Sandbox Code Playgroud)

或者

git --work-tree=repo1 (...)
Run Code Online (Sandbox Code Playgroud)

我想以这种方式执行所有git initgit 命令, event 。正确的做法是什么?

Ste*_*ker 10

Git 有一个-C <path>选项(如tar -C <path>)可以将 Git 的工作目录更改为path然后在该目录中执行命令。运行示例:

$ mkdir gitrepo
$ git -C gitrepo init
Initialized empty Git repository in /home/foo/gitrepo/.git/
Run Code Online (Sandbox Code Playgroud)

man git:

-C <path>

运行时就好像 git 是在<path>当前工作目录中启动的一样。-C当给出多个选项时,每个后续的非绝对-C <path>选项都会相对于前面的进行解释-C <path>

此选项会影响需要类似--git-dir和等路径名的选项--work-tree,因为它们对路径名的解释将相对于该-C选项引起的工作目录进行。例如,以下调用是等效的:

git --git-dir=a.git --work-tree=b -C c status
git --git-dir=c/a.git --work-tree=c/b status
Run Code Online (Sandbox Code Playgroud)

我的 Git 版本是 2.16.1。


opq*_*nut 4

您可以设置环境变量$GIT_DIR。查一下。