Git递归地将遥控器添加到子模块 [英] Git add remotes to submodules recursively
问题描述
我在计算机上的/path/to/repo
上有一个git repo,其中包含几个子模块/path/to/repo/submoduleA
和/path/to/repo/foo/bar/submoduleB
.
I have a git repo located on my machine at /path/to/repo
, which contains several submodules, /path/to/repo/submoduleA
and /path/to/repo/foo/bar/submoduleB
.
由于我无法更改工作流程,因此git repo被复制(如在scp -r中一样)复制到远程服务器上,我在该服务器上处理代码.我想将所做的更改拉回到原始计算机上.不能从远程服务器进行克隆/推送.
Due to a workflow that I cannot change, the git repo gets copied (as in scp -r) to a remote server, where I work on the code. I want to pull the changes back to the original machine. Cloning/pushing from the remote server isn't an option.
转到每个子模块并执行
git remote add <name> <url>:/server/path/to/repo/<path to submodule>
有更快的方法吗?神奇的东西
Is there a faster way? Something magical like
git remote add --submodules <name> <url>:/server/path/to/repo
会递归到每个子模块,并将适当的相对路径添加到每个子模块的远程位置? git remote --help
没有显示任何有用的内容,并且 Git专业版书子模块部分.
executed from the top-level repo that will recurse to each submodule and add the appropriate relative path onto the remote of each submodule? git remote --help
doesn't show anything useful, and neither the Git Pro Book section on submodules.
我最好的猜测是
git submodule foreach 'git remote add <name> <url>:/server/path/to/repo/...'
可能会起作用.我只是不知道git submodule foreach
might work, if there there is a way to replace the ...
with the loop-dependent relative path of each submodule in that foreach. I just don't know of such a mechanism built into git submodule foreach
推荐答案
git submodule foreach
does include a list of variables which should help:
该命令可以访问变量:
The command has access to the variables:
-
$name
:.gitmodules
中相关子模块节的名称, -
$sm_path
:直接超级项目中记录的子模块的路径 -
$displaypath
:包含从当前工作目录到子模块根目录的相对路径, -
$sha1
:直接超级项目中记录的提交,并且 -
$toplevel
:指向直接超级项目顶层的绝对路径. $name
: the name of the relevant submodule section in.gitmodules
,$sm_path
: the path of the submodule as recorded in the immediate superproject,$displaypath
: contains the relative path from the current working directory to the submodules root directory,$sha1
: the commit as recorded in the immediate superproject, and$toplevel
: the absolute path to the top-level of the immediate superproject.
所以在您的情况下:
git submodule foreach 'git remote add $name <url>:/server/path/to/repo/$sm_path'
$displaypath
会将...
替换为每个子模块的依赖于循环的相对路径.
但是,正如
$displaypath
would replace the ...
with the loop-dependent relative path of each submodule.
But, as noted by the OP waldol1 in the comments, $sm_path
is a fixed value, as opposed to a relative path.
在子文件夹中执行的 docker/docker.github.io
的示例 tests
:
D:\git\docker.github.io\tests>git submodule foreach "echo $displaypath"
Entering 'src/github.com/gdevillele/frontparser'
src/github.com/gdevillele/frontparser
vs.
D:\git\docker.github.io\tests>git submodule foreach "echo $sm_path"
Entering 'src/github.com/gdevillele/frontparser'
tests/src/github.com/gdevillele/frontparser
这篇关于Git递归地将遥控器添加到子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!