Git sub-tree merging back to the subtree for pushing to an upstream

While git sub-tree merge strategy works great for merging a library into vendor (when you want to pull HEAD on that library instead of waiting for releases), I had trouble finding documentation on pushing back to upstream.  This is annoying because one of the most obvious times you may want to use subtree is when you manage the library yourself, so can just pull straight from the repo, and want to push your maintenance changes back.

Surprisingly, and not what I have come to expect from git, it just works.  From the example in the docs I linked, when you are checked out on master

git merge --squash -s subtree --no-commit rack_branch

will merge the rack_branch into where you have specified.  But it just works the other way to.  If checked out on rack_branch

git merge --squash -s subtree --no-commit master

will do what you expect, so you can now just push your library branch back to it's upstream.

Awesome!

Filed under  //   git   tech  

Comments (4)

Aug 31, 2010
Charles said...
Thanks so much! Helped me from going crazy today.
Jan 08, 2011
Assuming my external repository is called "ext"

I did: git checkout ext/master
and then: git merge --squash -s subtree --no-commit master
and then pushed it to remote: git push git@codaset.com:gaminghorror/ext.git

I ended up with my entire project added to the ext repository. Do you have any idea what I've been doing wrong?

Feb 03, 2011
Tim Connor said...
Your git push command looks wacky and I have no idea what you are trying to do with it.
Feb 23, 2011
Jörg Seebohn said...
It works ! Great. Adding external libraries and merging changes back is really snappy now.

Leave a comment...