Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep a pushed/splitted subtree up-to-date?

Tags:

git

Imagine you have a repo 'A' with a subdirectory 'A/mySubDir' and you want to seperate 'A/mySubDir' into a new repo 'B'

git init B
cd <repo A>
git subtree split --prefix==A/mySubDir --branch=split
git push 'B' split:master

Assume HEAD on repo 'A' is now on 12fe. I update some files in 'A/mySubDir'.

How can I keep 'B' up-to-date when files in 'A/mySubDir' change?

git subtree split --prefix==A/mySubDir --branch=split 12fe..

ends in an error saying: Branch 'split' is not an anchestor of commit 'XXXX'

Does anybody have a glue on that.

Regards, Gert

like image 682
Gert Avatar asked Nov 08 '13 16:11

Gert


1 Answers

You have done the first step (splitting your A repo), but you haven't put B repo back into A, as explained in "Using Git subtrees for repository separation":

Here is an extract, adapted to your A/mySubDir - B situation:

Adding the repository as a subtree of your main repository

In your main repository, you need to get rid of the original files that you split, and then add the remote repository as a subtree instead.

Delete the entire directory you split from, and then commit.

git rm -r A/muSubdir
git commit -am "Remove split code."

Add the new shared repository as a remote

git remote add B /url/to/B.git

Now add the remote repository as a subtree

git subtree add --prefix=A/mySubDir --squash shared master

Note: we use the -–squash switch because we probably just want a single snapshot commit representing version X of the shared module, rather than complicating our own commit history with spurious upstream bugfix commits. Of course if you want the entire history then feel free to leave off that switch.

You now have subtree based on an upstream repository. Nice.

http://makingsoftware.files.wordpress.com/2013/02/gitsubtreeadd_thumb.png?w=500&h=196

In the image you can see the bottom commit is the squashed commit containing all the upstream code and this is merged with your code.

Important note: Do not be tempted to rebase this. Push it as is.
If you rebase, git subtree won’t be able to reconcile the commits when you do your next subtree pull.

So far so good. But this isn’t much use if you can’t receive changes from the upstream repository. Luckily that’s easy.

Pulling upstream changes

To pull changes from the upstream repository, just use the following command:

git subtree pull --prefix=A/mySubDir --squash shared master

(You are squashing all newer upstream commits into a single one that will then be merged into your repository).
Important: as mentioned above, do not rebase these commits.

Pushing changes to the upstream repository

Contributing changes to the upstream repository is as simple as:

git subtree push --prefix=A/mySubDir --squash shared master
like image 164
VonC Avatar answered Oct 11 '22 18:10

VonC