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

Re: Merging back from master but keeping a subtree

From
Jeff King <peff@peff.net>
Date
Sep 18, 2011, 03:37 UTC
Message-ID
<20110918033719.GB17977@sigill.intra.peff.net>
In-Reply-To
<87y5xn8v6z.fsf@dod.no>
On Sat, Sep 17, 2011 at 09:49:40AM +0200, Steinar Bang wrote:
Show 7 quoted lines
> I have a long lived branch that changes a directory and its
> subdirectory, ie. 
>  top/middle/mydirectory
> 
> Now I want to merge in an updated remoterepo/master and keep everything
> from that master, except for mydirectory and its subdirectory, where I
> would like to keep everything from my branch.

Git should generally do that automatically, unless both sides are changing mydirectory. In which case it will produce conflicts.

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

Show 10 quoted lines
> I tried a regular merge, and used
>  git checkout --ours
>  git add
> and 
>  git checkout --theirs
>  git add
> as appropriate on all conflicts.
> 
> But the result didn't build, and the build errors don't make much sense,
> so I think they are caused by "successful" merges giving bad results.

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):

  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:
       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
-Peff
Previous: Steinar BangNext: Steinar Bang
Message 2 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.