How to push to git subtree? How to push to git subtree? git git

How to push to git subtree?


So, with some joint effort with @PhilipKirkbride, here's what we managed to figure out:

You need to put all the details in the command:

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

Where master is the branch in the subtree, and subtree_origin is just another remote, which happens to point to your subtree's git repository. You can also type the full repo URL.


With Git 2.32 (Q2 2021), Dan's 2017 answer can be applied to any commit, not just HEAD.

See 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).
(Merged by Junio C Hamano -- gitster -- in commit 44ccb76, 10 May 2021)

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 now includes:

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.

That means this will work:

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

With Git 2.33 (Q3 2021), git subtree actually works on Windows.

See commit 77f37de, commit f7ee88f (14 Jun 2021) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit e22ac8b, 08 Jul 2021)

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