All of lore.kernel.org
 help / color / mirror / Atom feed
* Merging back from master but keeping a subtree
@ 2011-09-17  7:49 Steinar Bang
  2011-09-18  3:37 ` Jeff King
  0 siblings, 1 reply; 3+ messages in thread
From: Steinar Bang @ 2011-09-17  7:49 UTC (permalink / raw)
  To: git

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.

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.

Is there a better way to do this?

Would it be possible to unstage the already staged files and apply the
"checkout --ours" and "checkout --theirs", and then git add on the
checked out files?

Even that would be clumsy... I would have preferred something like
 git checkout --theirs top
 git checkout --ours top/middle/mydirectory
 git add-only-those-modified-wrt-my-branch


Thanks!

- Steinar

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Merging back from master but keeping a subtree
  2011-09-17  7:49 Merging back from master but keeping a subtree Steinar Bang
@ 2011-09-18  3:37 ` Jeff King
  2011-11-16 13:39   ` Steinar Bang
  0 siblings, 1 reply; 3+ messages in thread
From: Jeff King @ 2011-09-18  3:37 UTC (permalink / raw)
  To: git

On Sat, Sep 17, 2011 at 09:49:40AM +0200, Steinar Bang wrote:

> 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?

> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Merging back from master but keeping a subtree
  2011-09-18  3:37 ` Jeff King
@ 2011-11-16 13:39   ` Steinar Bang
  0 siblings, 0 replies; 3+ messages in thread
From: Steinar Bang @ 2011-11-16 13:39 UTC (permalink / raw)
  To: git

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-11-16 13:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-17  7:49 Merging back from master but keeping a subtree Steinar Bang
2011-09-18  3:37 ` Jeff King
2011-11-16 13:39   ` Steinar Bang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.