如何推送到 git 子树?

How to push to git subtree?

我在 git 上添加了一个似乎有效的子树。我进入目录并进行了一些更改,然后推送。

我注意到这些更改显示在父项目中,但我用作源的存储库尚未更新。

我试过 git subtree push returns:

You must provide the --prefix option.

还有 git subtree push --prefix=my/folder git@mygitrepo.com 其中 returns:

You must provide <repository> <ref>

将更改推送到我的 git 子树的原始存储库的正确方法是什么?

编辑:有人建议我这样做:

git subtree push --prefix=my/folder origin master

当我 运行 上面的内容时,它开始了这个过程,但最终说分支在后面。所以我 运行:

git subtree pull --prefix=my/folder origin master

哪个returns"Already up to date"

因此,通过与@PhilipKirkbride 的共同努力,我们设法弄清楚了以下内容:

您需要在命令中输入所有详细信息:

git subtree push --prefix=my/folder subtree_origin master

其中 master 是子树中的分支,subtree_origin 只是另一个远程节点,恰好指向子树的 git 存储库。您还可以输入完整的 repo URL.

使用 Git 2.32(2021 年第二季度),Dan's 2017 可以应用于任何提交,而不仅仅是 HEAD。

参见 commit 9a3e3ca, commit 49470cd, commit 94389e7, commit cb65514, commit 6468784, commit e9525a8, commit 534ff90, commit 5cdae0f, commit cbb5de8, commit e4f8baa, commit bbffb02, commit 22d5507, commit a94f911, commit e2b11e4, commit 6d43585, commit f664304, commit 8dc3240, commit d2f0f81, commit 5a35697, commit b04538d, commit b269976, commit db6952b, commit f1cd2d9, commit 63ac4f1, commit c4566ab, commit 40b1e1e, commit f700406, commit f2bb7fe, commit 914d512, commit 4c996de (27 Apr 2021) by Luke Shumaker (LukeShu)
(由 Junio C Hamano -- gitster -- in commit 44ccb76 合并,2021 年 5 月 10 日)

subtree: push: allow specifying a local rev other than HEAD

Signed-off-by: Luke Shumaker

git subtree split lets you specify a rev other than HEAD.
'git push'(man) lets you specify a mapping between a local thing and a remot ref.

So smash those together, and have git subtree push let you specify which local thing to run split on and push the result of that split to the remote ref.

git subtree man page 现在包括:

push <repository> [+][<local-commit>:]<remote-ref>

Does a 'split' using the subtree of <local-commit> and then does a 'git push' to push the result to the <repository> and <remote-ref>.

This can be used to push your subtree to different branches of the remote repository.
Just as with 'split', if no <local-commit> is given, then HEAD is used.
The optional leading '+' is ignored.

这意味着这会起作用:

git subtree push --prefix="sub dir" --annotate="*" ./"sub proj" HEAD^:from-mainline
                                                                ^^^^^^

对于 Git 2.33(2021 年第 3 季度),git subtree 实际上适用于 Windows。

参见 commit 77f37de, commit f7ee88f (14 Jun 2021) by Johannes Schindelin (dscho)
(由 Junio C Hamano -- gitster -- in commit e22ac8b 合并,2021 年 7 月 8 日)

subtree: fix the GIT_EXEC_PATH sanity check to work on Windows

Signed-off-by: Johannes Schindelin

In 22d5507 ("subtree: don't fuss with PATH", 2021-04-27, Git v2.32.0-rc0 -- merge listed in batch #15), git subtree was broken thoroughly on Windows.

The reason is that it assumes Unix semantics, where PATH is colon-separated, and it assumes that $GIT_EXEC_PATH: is a verbatim prefix of $PATH.
Neither are true, the latter in particular because GIT_EXEC_PATH is a Windows-style path, while PATH is a Unix-style path list.

Let's make extra certain that $GIT_EXEC_PATH and the first component of $PATH refer to different entities before errorring out.

We do that by using the test <path1> -ef <path2> command that verifies that the inode of <path1> and of <path2> is the same.

Sadly, this construct is non-portable, according to test utility.
However, it does not matter in practice because we still first look whether $GIT_EXEC_PREFIX is string-identical to the first component of $PATH.
This will give us the expected result everywhere but in Git for Windows, and Git for Windows' own Bash does handle the -ef operator.

Just in case that we do need to show the error message and are running in a shell that lacks support for -ef, we simply suppress the error output for that part.

This fixes git-for-windows/git issue 3260


使用 Git 2.35(2022 年第一季度),git subtree(在 contrib/ 中)更快。

参见 commit 3ce8888 (08 Dec 2021) by James Limbouris (jamesl-dm)
(由 Junio C Hamano -- gitster -- in commit 9b6eda0 合并,2022 年 1 月 10 日)

subtree: fix argument handling in check_parents

Signed-off-by: James Limbouris

315a84f ("subtree: use commits before rejoins for splits", 2018-09-28, Git v2.20.0-rc0 -- merge listed in batch #6) changed the signature of check_parents from 'check_parents [REV...]' to 'check_parents PARENTS_EXPR INDENT'.
In other words the variable list of parent revisions became a list embedded in a string.
However it neglected to unpack the list again before sending it to cache_miss, leading to incorrect calls whenever more than one parent was present.
This is the case whenever a merge commit is processed, with the end result being a loss of performance from unecessary rechecks.

The indent parameter was subsequently removed in e9525a8 ("subtree: have $indent actually affect indentation", 2021-04-27, Git v2.32.0-rc0 -- merge listed in batch #15), but the argument handling bug remained.

For consistency, take multiple arguments in check_parents, and pass all of them to cache_miss separately.

通常输出目录,例如 distpublic 在 .gitignore 中被忽略。如果你想在这些路径上使用git subtree,你必须

git add <DIR to be subtree pushed>

然后 git commit

完成上述两个步骤后,您可以git subtree push将那些生成的目录放入您的存储库的单独分支中。