在“远程”目录中执行Git pull

4
在Git中,你可以将给定的目录克隆到指定的目录:
git clone ssh://gitolite@dev.bipper.com:3687/com/bipper/kids/portal <dir>

由于我们运行构建脚本时使用的“cd”命令存在一些复杂的问题,因此我想执行类似于以下内容的git pull操作:
git pull <dir-to-the-git-folder>

因此,在执行拉取(或获取/合并)时不必在git文件夹中,是否有可能以某种方式实现(我的上面的示例不起作用)?
3个回答

4
我认为您应该查看git-dir和也许是work-tree选项,如果您愿意,您也可以使用GIT_DIR/GIT_WORK_TREE变量。请参阅手册:

--git-dir=<path>设置存储库路径。这也可以通过设置GIT_DIR环境变量进行控制。它可以是绝对路径或相对于当前工作目录的路径。

-work-tree=<path>设置工作树路径。它可以是绝对路径或相对于当前工作目录的路径。这也可以通过设置GIT_WORK_TREE环境变量和core.worktree配置变量(有关更详细的讨论,请参见git-config(1)中的core.worktree)来进行控制。


嗯,顺便说一句,我们正在尝试在我们的Linux服务器上执行以下操作:git --git-dir=/tmp/deployment_1347960691855 --work-tree=/tmp/deployment_1347960691855 pull但是出现了以下错误: WARN:fatal: Not a git repository: '/tmp/deployment_1347960691855'但它确实是一个Git仓库。 - Thomas Vervik
还尝试在我本地的Windows机器上使用另一个Git项目进行了测试:C:>git --git-dir=C:\projectsGit\bsafe-server --work-tree=C:\projectsGit\bsafe-server pull 致命错误: 'C:\projectsGit\bsafe-server' 不是git存储库。 - Thomas Vervik
1
也许你应该尝试使用 C:\projectsGit\basfe-server\.git 作为 git-dir 选项... 参见这个问题:https://dev59.com/VXM_5IYBdhLWcg3waieJ 尽管这不是手册上所说的... - greg0ire

1
从git 1.8.5开始,您可以使用-C选项更改目录上下文。
sudo -u www-data -H git -C /var/www/ status

-H 会为您的命令设置主目录。这是可选的,但当您想要避免以下错误时非常有用:

warning: unable to access '/root/.config/git/attributes': Permission denied
or
warning: unable to access '/home/user/.config/git/attributes': Permission denied

1

尝试

GIT_DIR=<dir-to-the-git-folder>/.git GIT_WORK_TREE=<dir-to-the-git-folder> git pull

您还可以使用--git-dir--work-tree选项来指定这些选项。

来自man git

   --git-dir=<path>
       Set the path to the repository. This can also be controlled by setting the GIT_DIR environment
       variable. It can be an absolute path or relative path to current working directory.

   --work-tree=<path>
       Set the path to the working tree. It can be an absolute path or a path relative to the current
       working directory. This can also be controlled by setting the GIT_WORK_TREE environment variable
       and the core.worktree configuration variable (see core.worktree in git-config(1) for a more
       detailed discussion).

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接