git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Merging back from master but keeping a subtree

From
SBSteinar Bang <sb@dod.no>
Date
Nov 16, 2011, 13:39 UTC
Message-ID
<87aa7wrxsz.fsf@dod.no>
In-Reply-To
<20110918033719.GB17977@sigill.intra.peff.net>
>>>>> Jeff King <peff@peff.net>:
> Git should generally do that automatically, unless both sides are
> changing mydirectory. In which case it will produce conflicts.

I thought so too, but the end result didn't build, and I was unable to figure out why.

> Are you sure you really want to just throw out what the other side did
> in mydirectory?

Yes, that's the only thing I'm sure about. All changes to mydirectory should come from my branch. If there are changes necessary to make things build, they are better done by me.

> If git was able to auto-merge some files, then they will not be marked
> as conflicts in the index. And "git checkout --ours" is about looking in
> the index for conflicted entries, and then selecting one side.
> I think what you want instead is to do is (assuming you really want to
> throw out their side):
Thanks for the suggestion.  I tried following this approach, but...
>   1. Start a merge between them and us:
>        git merge --no-commit remoterepo/master
>   2. Throw out whatever the merge came up with and make it look like
>      their tree:

...I never quite could figure out if I did the right thing here. Ie. when throwing out what the commit came up with.

>        git checkout remoterepo/master -- top
>   3. Now overwrite their version of mydirectory with what was in your
>      branch:
>        git checkout HEAD -- top/middle/mydirectory
>   4. Commit the resulting tree:
>        git commit

The problem was that the end result didn't build, wit pretty much the same obscure failures that the regular merge had.

But I eventually tried something that worked:
 1. First create a local branch off master
 2. Merge in mybranch with "-s ours --no-commit"
 3. Checkout mydirectory from mybranch
 4. Commit
 5. Switch to mybranch
 6. Merge in the new local branch, which resulted in a conflict-free
    merge that has my changes in mydirectory and the rest of the world
    in the rest of the working directory, and the history of mydirectory
    looks ok
The end result built and worked.

If git had had a "-s theirs" strategy, I wouldn't have needed the temporary branch made off master. That need for that branch is probably the most cryptic thing for people following my instructions for future merges.

To sum up the commands:
 git checkout master
 git checkout -b master_nicely_merged_with_mybranch
 git merge -s ours --no-commit origin/mybranch
 git checkout origin/mybranch top/middle/mydirectory
 git commit
 git checkout mybranch
 git merge master_nicely_merged_with_mybranch
Thanks!
- Steinar
Previous: Jeff King
Message 3 of 3 in “Merging back from master but keeping a subtree”
  1. Steinar BangSep 17, 2011
  2. Jeff KingSep 18, 2011
  3. Steinar BangNov 16, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.