简体   繁体   中英

Git subtree merge removes changes in branch being merged to

I setup two repositories using the structure described in the Git book here: http://git-scm.com/book/ch6-7.html . Basically, I wanted to keep a relationship between the copy of one repo and it's master branch on origin. I know you can do this with submodules but the process of updating them seemed a bit cumbersome, and I was told (and read) that subtrees were easier to work with. Anyways, I setup "RepoA" to have a subtree of "RepoB" in it. I followed the steps provided in the link to a tee.

I've made major modifications to the subtree in "RepoA". At the same time, other people on my team are making modifications to the master on "RepoB". When all is said and I done, I expect that I can pull the changes from "RepoB" origin/master and subtree merge them into the subtree in RepoA. However, when I do this (using the -s subtree merge option with --squash and --no-commit), all of the changes I made to the subtree in RepoA are removed. Even new files that I added to the subtree are deleted! Why is this? Did I misunderstand the use of a subtree? Am I merging it improperly? Any help would be greatly appreciated!

Additionally, if it's impossible to properly merge these with a subtree merge, is my only choice for merging them doing it by hand, manually, and creating a new project structure?

Thanks!

Assuming that you already set up your RepoB as a remote, try this:

git merge -s ours --no-commit RepoB/master

More information here: https://help.github.com/articles/working-with-subtree-merge

The 'git read-tree' step of subtree merge of RepoB into RepoA does not work if the RepoB subtree is present. Skipping the read-tree results in all files being locally deleted. (start over)

The RepoB subtree must be locally removed from git. After that, the git-read-tree will work, and the merge will be correct for both files and history.

Starting with a clone of RepoA with a RepoB_subtree from a previous merge.

git remote add -f RepoB <RepoB-url>
git merge -s ours  --no-commit RepoB/master
git rm -r RepoB_subtree
git read-tree --prefix=RepoB_subtree/ -u RepoB/master
git commit -m "Subtree merge of RepoB/master."

The RepoA history shows the entire block of RepoB commits first. The previous subtree merge commits are shown in RepoA without any RepoB history.

The organization of the RepoB history in RepoA wasn't linear in time as I expected, but in retrospect, the history of the RepoB subtree in RepoA was complete and easy to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM