* unmerging feature branches
@ 2007-10-23 15:24 martin f krafft
2007-10-23 16:19 ` Matthieu Moy
2007-10-23 16:50 ` Linus Torvalds
0 siblings, 2 replies; 16+ messages in thread
From: martin f krafft @ 2007-10-23 15:24 UTC (permalink / raw)
To: git discussion list
[-- Attachment #1: Type: text/plain, Size: 1168 bytes --]
Dear list,
Let's say I developed a feature Foo on a branch off master, and at
some point I merged it back into master (commit M) and published the
repo. Since M, a number of commits have been made onto master.
Now I woul like to undo the merge.
I could rebase (M+1)..master onto M^ (on the former master branch),
but that would orphan the commits between the merge point and the
tip of master, which others are tracking.
I'd love to have git-revert, but that cannot undo a multi-parent
commit.
I could git-revert every commit on the feature branch between the
branch point and the merge point, even squash them into a single
commit, but that is a lot of work.
Are there any other methods? Is it conceivable to let git-revert
revert a merging commit if you tell it somehow which of the two (or
more) parents are the ones you want undone, meaning that you'd like
to keep the others?
--
martin | http://madduck.net/ | http://two.sentenc.es/
"a man who does not realise
that he is half an animal
is only half a man."
-- thornton wilder
spamtraps: madduck.bogus@madduck.net
[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 15:24 unmerging feature branches martin f krafft
@ 2007-10-23 16:19 ` Matthieu Moy
2007-10-23 16:50 ` Linus Torvalds
1 sibling, 0 replies; 16+ messages in thread
From: Matthieu Moy @ 2007-10-23 16:19 UTC (permalink / raw)
To: martin f krafft; +Cc: git discussion list
martin f krafft <madduck@madduck.net> writes:
> Now I woul like to undo the merge.
Dirty solution: export the patch corresponding to the merge (diff
M..M^), and apply it on master. If you have no conflicts, it should be
doable. If you have conflicts, it will probably be painfull.
--
Matthieu
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 15:24 unmerging feature branches martin f krafft
2007-10-23 16:19 ` Matthieu Moy
@ 2007-10-23 16:50 ` Linus Torvalds
2007-10-23 17:16 ` martin f krafft
2007-10-23 19:33 ` Junio C Hamano
1 sibling, 2 replies; 16+ messages in thread
From: Linus Torvalds @ 2007-10-23 16:50 UTC (permalink / raw)
To: martin f krafft; +Cc: git discussion list
On Tue, 23 Oct 2007, martin f krafft wrote:
>
> Are there any other methods? Is it conceivable to let git-revert
> revert a merging commit if you tell it somehow which of the two (or
> more) parents are the ones you want undone, meaning that you'd like
> to keep the others?
So let me get this straight.. You have a merge "M" that is the result of
merging (possibly multiple) topic branches, and you now want to undo the
part that *one* of them brought in?
First off, let me say that to some degree, what you ask for is not
possible. Why?
Since you have pushed out the stuff, and don't want to rewrite history
(which would result in trouble for down-streams - and I heartily approve),
whatever you do will always have that merge in the commit history.
And that means that while you can certainly undo the *data* that the merge
brought in, git will always know that you already merged up that branch.
Which means that if you later decide that you *do* want to do the merge
after all, you now really cannot - trying to merge the branch later on
will just be a fast-forward, and you'll never get the actual changes from
that merge (since git knows you already have them!).
So you can revert the data, but then if you want to get it back, you'll
need to revert the revert - you cannot just merge the branch again.
So the first thing you need to realize is that "revert" does not revert
history, it *only* reverts data. The fact that you did the merge will
always remain, although you could try to hack around even that by using
the 'grafts' file and trying to hide it (I really don't think it's a good
idea, but sure, everything is "possible" in that sense).
Now, that said, reverting the data is not that hard. There is not any
single-command "revert this arm of a merge", but on the other hand, git
can certainly help you.
The way to do it is:
# go back to just before the merge, create a "fixup" branch
#
git branch -b fixup M^
# merge all of it again, *except* the branch you didn't want to
# merge (this example assumes that you had a four-way octopus
# merge, and you now want to turn it into a three-way with the
# next-to-last parent skipped):
#
git merge -m "fixed merge" M^2 M^4
# You now have "fixup" containing what you *wanted* it to be
# after the original merge. Create a temporary branch that is
# based on the merge and contains that state instead, and
# apply the difference.
#
git branch -b temporary M
git diff ..fixup | git-apply
git commit -m "fixup commit"
# You now have the "temporary" branch that contains just the
# diff that effectively undoes that one merge. Go back to the
# tip of your development, and cherry-pick it to get git to
# help you do a good job merging it with all the subsequent
# development
#
git checkout master # or whatever branch you used
git cherry-pick temporary
.. do whatever you need to do to resolve it
.. if it didn't go cleanly
# Now, edit the commit message to talk about what you did
#
git commit --amend
or something to that effect.
Complicated? Yes. The above is strictly speaking more complex than you may
need, but if you do it like the above, you get maximum help from git (ie
you *could* have tried to just apply the patch with "git-apply" directly
on the top of master, but if you do it like the above, then it's
guaranteed that the patch that undoes the commit will apply cleanly, and
you then use "git cherry-pick" which uses the merge logic that can do a
proper three-way merge with renames etc, so if there are conflicts or
other things, the above will likely be the best way to do it)
So for simple cases, you can do the above more simply, but the above is
fairly brainless and scriptable except for the *one* place where you
actually move the changes forward (the single cherry-pick).
There are certainly other ways too. You could just "git revert -n" all the
commits that came in through the branch you didn't want to merge. That
doesn't work well if there were merges in that area, though, or of there
were changes that were common to all the branches (some of which also came
in through *other* merges).
So the above (UNTESTED! Caveat emptor!) sequence is *one* way of doing it,
and probably in the end the one that most closely represents what you want
to do.
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 16:50 ` Linus Torvalds
@ 2007-10-23 17:16 ` martin f krafft
2007-10-23 17:40 ` Linus Torvalds
2007-10-23 19:33 ` Junio C Hamano
1 sibling, 1 reply; 16+ messages in thread
From: martin f krafft @ 2007-10-23 17:16 UTC (permalink / raw)
To: Linus Torvalds, git discussion list
[-- Attachment #1: Type: text/plain, Size: 1567 bytes --]
also sprach Linus Torvalds <torvalds@linux-foundation.org> [2007.10.23.1850 +0200]:
> First off, let me say that to some degree, what you ask for is not
> possible. Why?
>
> Since you have pushed out the stuff, and don't want to rewrite history
> (which would result in trouble for down-streams - and I heartily approve),
> whatever you do will always have that merge in the commit history.
>
> And that means that while you can certainly undo the *data* that the merge
> brought in, git will always know that you already merged up that branch.
This is precisely what I meant, sorry for not being clear. This is
what git-revert does...
> So you can revert the data, but then if you want to get it back, you'll
> need to revert the revert - you cannot just merge the branch again.
Ouch!
> # You now have the "temporary" branch that contains just the
> # diff that effectively undoes that one merge. Go back to the
> # tip of your development, and cherry-pick it to get git to
> # help you do a good job merging it with all the subsequent
> # development
Ah, that's a good idea.
Thanks for your time and input!
PS: this question of mine came out of a discussion on using Git for
Debian packaging: what happens when we actually need to remove
a feature from one package to the next:
http://lists.madduck.net/pipermail/vcs-pkg/2007-October/000059.html
--
martin | http://madduck.net/ | http://two.sentenc.es/
#define emacs eighty megabytes and constantly swapping.
spamtraps: madduck.bogus@madduck.net
[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 17:16 ` martin f krafft
@ 2007-10-23 17:40 ` Linus Torvalds
2007-10-23 18:08 ` martin f krafft
2007-10-31 21:16 ` Alejandro Martinez Ruiz
0 siblings, 2 replies; 16+ messages in thread
From: Linus Torvalds @ 2007-10-23 17:40 UTC (permalink / raw)
To: martin f krafft; +Cc: git discussion list
On Tue, 23 Oct 2007, martin f krafft wrote:
>
> > So you can revert the data, but then if you want to get it back, you'll
> > need to revert the revert - you cannot just merge the branch again.
>
> Ouch!
Well, it's not necessarily "Ouch".
It actually depends on what you want to do. Sometimes this is a feature,
and the thing is, it actually works for other things that just merge
commits.
In other words, think of what happens when you merge some development
branch, and then "git revert" a single commit from that branch - the exact
same thing will happen - future merges of that branch will *not* re-do the
commit, because you "already have it", and you reverted it after-the-fact.
And in many ways, this is "obviously" what you want to happen!
Now, I say "obviously" in quotes, because it's not at all obvious in an
absolute sense - it may be that you reverted the commit not because it was
buggy, but because your stable branch wasn't ready for it yet, and maybe
in the future you do actually want the code that the revert reverted. So
in that sense, nothing is really "obvious", and this is simply how things
work. But I think that it's easier to explain why git does something like
this when you speak about normal commits, and it all makes sense.
When you revert the data from a merge, the exact same issue happens. A
revert (whether done by "git revert", or by the sequence of events I
described) very fundamentally undoes the *data* part, but leaves the
history intact, and that has implications for future events that think
about history - which is mostly "git merge", but there are other thigns
too.
As an example of "other things" that take history into account, think
about something like "git rebase". It's not a merge, but it also takes
history into account in certain ways: in particular, it may be effectively
a "series of cherry-picks", but it actually takes the history of both
branches into account, and will not re-apply a patch that already exists
in the target history.
What does that mean? Let's say that both histories contain a patch X (not
the same commit, but the same patch), but one history also contains the
revert of X. Again, the revert reverts the data, but it does *not* revert
the history, so when you cherry-pick all the stuff from the other branch,
X will *not* happen - even if it would apply cleanly, and even if a plain
"git cherry-pick" would have redone it!
Why? History, again. Because "git rebase" sees that the commit already
existed, it won't even try to apply it again, never mind that it could
have worked. The "revert" didn't undo the history, just the data.
So a "revert" is fundamentally different from a "undo". Most of the time
that's exactly what you want, and I'm not pointing this out as a problem,
I just wanted to point out that it has "effects". Sometimes the effects
are good, sometimes they are bad, and while they are always very reliable
and there's never any question about what git will do, people don't always
think like git, and whether the effects are "good" or "bad" is probably
entirely up to whether they match users expectations or not.
So sometimes the behaviour of "git revert" will be exactly what people
expected and wanted ("good, I'll never get that commit again when I pull,
because I told git that I don't want that commit"), and sometimes it will
_not_ be what people expected and wanted ("oh, I didn't get that commit,
even though I was now ready for it - because I had reverted it back when I
was *not* ready for it").
See? The logic is exactly the same in both cases, but one was good, the
other bad, and the only difference was really the mindset of the user.
A tool can't ever get "mindset of the user" differences right. At least
not until we add the "esp option" ;)
So I really don't want to push this as a problem or deficiency, I think
it's a good thing. But it's a good thing only when people are *aware* of
what "revert" really means.
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 17:40 ` Linus Torvalds
@ 2007-10-23 18:08 ` martin f krafft
2007-10-23 18:24 ` Linus Torvalds
2007-10-31 21:16 ` Alejandro Martinez Ruiz
1 sibling, 1 reply; 16+ messages in thread
From: martin f krafft @ 2007-10-23 18:08 UTC (permalink / raw)
To: Linus Torvalds, git discussion list
[-- Attachment #1: Type: text/plain, Size: 4097 bytes --]
also sprach Linus Torvalds <torvalds@linux-foundation.org> [2007.10.23.1940 +0200]:
> > > So you can revert the data, but then if you want to get it back, you'll
> > > need to revert the revert - you cannot just merge the branch again.
> >
> > Ouch!
>
> Well, it's not necessarily "Ouch".
I said "ouch" only because I can foresee the confusion this may
cause in collaborative package maintenance. One party merges the
feature branch, another reverts it, and a third (or the first)
wonders why the feature isn't present despite having merged the
branch and must go through history to find the reverting commit,
which is tied to the commit it reverts through nothing else than
a log message, at best.
> In other words, think of what happens when you merge some
> development branch, and then "git revert" a single commit from
> that branch - the exact same thing will happen - future merges of
> that branch will *not* re-do the commit, because you "already have
> it", and you reverted it after-the-fact.
[...]
> When you revert the data from a merge, the exact same issue
> happens. A revert (whether done by "git revert", or by the
> sequence of events I described) very fundamentally undoes the
> *data* part, but leaves the history intact, and that has
> implications for future events that think about history - which is
> mostly "git merge", but there are other thigns too.
While this makes perfect sense, I am a bit thrown off now wrt two
earlier posts by you (in another thread), where you said:
In other words, git never looks at individual commits when trying
to merge. It doesn't try to figure out what the "meaning" of the
changes are, it purely looks at the content.
-- http://marc.info/?l=git&m=119198488411957&w=2
Yes, history is interesting for historical reasons, and to explain
what the context was, but in many ways, history is exactly the
*wrong* thing to use when it comes to merging. You should look at
the end result, since people can - and do - come to the same
result through different ways.
-- http://marc.info/?l=git&m=119204501428555&w=2
I master merged branch Foo, then reverted a commit introduced by
Foo, and then Foo would be re-merged, the content *will* differ. So
Git *has to* look at the list of commits in history to properly
handle reverts and *not* redo commits which have since been
reverted.
Is this correct?
> As an example of "other things" that take history into account, think
> about something like "git rebase". It's not a merge, but it also takes
> history into account in certain ways: in particular, it may be effectively
> a "series of cherry-picks", but it actually takes the history of both
> branches into account, and will not re-apply a patch that already exists
> in the target history.
In the light of the discussion in
(http://marc.info/?t=119198137100002&r=1&w=2), I am now completely
confused. Or well, not confused, but I simply don't know anymore
what Git does, and I thought I did.
> What does that mean? Let's say that both histories contain a patch X (not
> the same commit, but the same patch), but one history also contains the
> revert of X. Again, the revert reverts the data, but it does *not* revert
> the history, so when you cherry-pick all the stuff from the other branch,
> X will *not* happen - even if it would apply cleanly, and even if a plain
> "git cherry-pick" would have redone it!
>
> Why? History, again. Because "git rebase" sees that the commit already
> existed, it won't even try to apply it again, never mind that it could
> have worked. The "revert" didn't undo the history, just the data.
How can rebase know that the commit already existed when you're
saying above that it's about patch X, *not* the same commit?
--
martin | http://madduck.net/ | http://two.sentenc.es/
"a woman begins by resisting a man's advances and ends by blocking
his retreat."
-- oscar wilde
spamtraps: madduck.bogus@madduck.net
[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 18:08 ` martin f krafft
@ 2007-10-23 18:24 ` Linus Torvalds
2007-10-23 19:17 ` martin f krafft
0 siblings, 1 reply; 16+ messages in thread
From: Linus Torvalds @ 2007-10-23 18:24 UTC (permalink / raw)
To: martin f krafft; +Cc: git discussion list
On Tue, 23 Oct 2007, martin f krafft wrote:
>
> While this makes perfect sense, I am a bit thrown off now wrt two
> earlier posts by you (in another thread), where you said:
>
> In other words, git never looks at individual commits when trying
> to merge. It doesn't try to figure out what the "meaning" of the
> changes are, it purely looks at the content.
> -- http://marc.info/?l=git&m=119198488411957&w=2
This is still true.
Git never looks at individual commits when merging, it looks at the
*history*.
So it does look at the commits only in the sense that it uses the "shape"
of the history (which is obviously built up from many individual commits!)
but it never looks at any individual commit per se.
And the behaviour of "git revert" comes *exactly* from the fact that git
never even bothers to look at the revert as a "revert" of history! A
revert is a normal data commit, and has absolutely zero impact on history
itself. So the reason a merge will never give "back" the data over a
revert is that the data was already merged, since the history itself
didn't change!
> I master merged branch Foo, then reverted a commit introduced by
> Foo, and then Foo would be re-merged, the content *will* differ.
No. If you re-merge Foo, nothing at all happens! You're already merged.
It's a no-op.
If Foo has had *new* commits in the meantime, those new commits will show
up, of course, but the old commits have absolutely zero effect, because
they will be part of the common history.
> So Git *has to* look at the list of commits in history to properly
> handle reverts and *not* redo commits which have since been reverted.
>
> Is this correct?
No, that's absolutely incorrect. You didn't understand what I meant.
Git merge doesn't look at the revert at all, except (indorectly) when it
builds up the history and it passes over it in order to find the common
base for the history.
> > As an example of "other things" that take history into account, think
> > about something like "git rebase". It's not a merge, but it also takes
> > history into account in certain ways: in particular, it may be effectively
> > a "series of cherry-picks", but it actually takes the history of both
> > branches into account, and will not re-apply a patch that already exists
> > in the target history.
>
> In the light of the discussion in
> (http://marc.info/?t=119198137100002&r=1&w=2), I am now completely
> confused. Or well, not confused, but I simply don't know anymore
> what Git does, and I thought I did.
git-rebase is special. It really does look at each commit (obviously),
since it needs to *move* each commit.
So git-rebase has nothing at all to do with merges. They have similar
behaviour (quite often the end result is identical in the *data* - at
least that's the good case), but at the same time they are very different
indeed.
Git-merge *only* cares about the "shape of the history" (to find the
common commit(s) to use as a merge base) and the actual data, while "git
rebase" actually goes one commit at a time.
> How can rebase know that the commit already existed when you're
> saying above that it's about patch X, *not* the same commit?
Git-rebase looks at the patch itself, using the "patch fingerprint", and
when it goes through each commit, it skips commits that have already been
applied.
See the man-pages for "git-cherry" and "git-patch-id".
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 18:24 ` Linus Torvalds
@ 2007-10-23 19:17 ` martin f krafft
2007-10-23 19:38 ` Linus Torvalds
0 siblings, 1 reply; 16+ messages in thread
From: martin f krafft @ 2007-10-23 19:17 UTC (permalink / raw)
To: Linus Torvalds, git discussion list
[-- Attachment #1: Type: text/plain, Size: 2016 bytes --]
also sprach Linus Torvalds <torvalds@linux-foundation.org> [2007.10.23.2024 +0200]:
> So it does look at the commits only in the sense that it uses the "shape"
> of the history (which is obviously built up from many individual commits!)
> but it never looks at any individual commit per se.
I don't follow what you mean with "shape". The following is
a history:
o - x - o - o - o - m - o - A* - o - m2 - o - master
\ / /
`o - A - L -' - F - o - o - T' - branch
A is a commit, A* is the commit which reverts (the data change by)
A. L and F are to mark the last and first commits before and after
the first merge m. T is the tip of 'branch'
After merge point m2, the change introduced by A will *not* be in
master. This much makes sense.
What did not make sense is how Git determines to leave it out. But
I think that after drawing the above, it's now clear:
by shape you mean the actual graph, and when 'branch' is merged into
master at m2, Git goes back in time to conclude that master...L must
already be present in master due to the intersection of the two
lines at m, and thus finds commit F as the "oldest direct
descendant" of m2. L is an older descendant of m2, but it's not
direct in the sense that there are multiple paths from m2 to L. Thus
Git will only merge F..T at m2.
Or as you put it:
> If Foo has had *new* commits in the meantime, those new commits
> will show up, of course, but the old commits have absolutely zero
> effect, because they will be part of the common history.
I think I am (moderately) clear again on the inner working of Git.
Sorry for the confusion.
--
martin | http://madduck.net/ | http://two.sentenc.es/
"the search for the perfect martini is a fraud. the perfect martini
is a belt of gin from the bottle; anything else is the decadent
trappings of civilization."
-- t. k.
spamtraps: madduck.bogus@madduck.net
[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 16:50 ` Linus Torvalds
2007-10-23 17:16 ` martin f krafft
@ 2007-10-23 19:33 ` Junio C Hamano
2007-10-23 19:49 ` Linus Torvalds
1 sibling, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2007-10-23 19:33 UTC (permalink / raw)
To: Linus Torvalds; +Cc: martin f krafft, git discussion list
Linus Torvalds <torvalds@linux-foundation.org> writes:
> Now, that said, reverting the data is not that hard. There is not any
> single-command "revert this arm of a merge", but on the other hand, git
> can certainly help you.
>
> The way to do it is:
>
> # go back to just before the merge, create a "fixup" branch
> #
> git branch -b fixup M^
>
> # merge all of it again, *except* the branch you didn't want to
> # merge (this example assumes that you had a four-way octopus
> # merge, and you now want to turn it into a three-way with the
> # next-to-last parent skipped):
> ...
Desire to revert an octopus would, as you demonstrated, often be
to revert only one arm, but I think allowing to revert a twohead
merge should be trivial. If we define "reverting a merge" to
always revert all arms, then this should suffice.
diff --git a/builtin-revert.c b/builtin-revert.c
index a655c8e..719e293 100644
--- a/builtin-revert.c
+++ b/builtin-revert.c
@@ -269,8 +269,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
if (!commit->parents)
die ("Cannot %s a root commit", me);
- if (commit->parents->next)
- die ("Cannot %s a multi-parent commit.", me);
+ if (action != REVERT && commit->parents->next)
+ die ("Cannot %s a merge commit.", me);
if (!(message = commit->buffer))
die ("Cannot get commit message for %s",
sha1_to_hex(commit->object.sha1));
Note that allowing cherry-pick by removing the above two lines
allow replaying the data of a merge similar to a squash merge.
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 19:17 ` martin f krafft
@ 2007-10-23 19:38 ` Linus Torvalds
2007-10-23 19:46 ` Linus Torvalds
0 siblings, 1 reply; 16+ messages in thread
From: Linus Torvalds @ 2007-10-23 19:38 UTC (permalink / raw)
To: martin f krafft; +Cc: git discussion list
On Tue, 23 Oct 2007, martin f krafft wrote:
>
> I don't follow what you mean with "shape". The following is
> a history:
>
> o - x - o - o - o - m - o - A* - o - m2 - o - master
> \ / /
> `o - A - L -' - F - o - o - T' - branch
Right. And you can do two things when merging:
- the wrong and insane thing: look at the *contents* of each commit, to
decide if a commit does a certain thing.
- the git thing: never look at individual commits at all, except to find
the global history, what I called the "shape".
So git obviously very much *does* look at history, but it does so only to
find the common point. So in when you create "m2", it looked at the
history to see that the common point of the two branches was "L". And once
it has found that, all the other stuff is totally irrelevant. It doesn't
matter if there are a million commits between 'm' and 'm2', the only thing
that mattered was the "topology" aka "shape" of the history.
See?
So git never actually cares about the individual commits A*, F, and T (or
anything else) when it merges those two histories and created "m2".
It did *traverse* those commits, in order to find that "Oh, the last
common state was 'L'", but it never looked at them in any other sense.
They were all individually uninteresting, and the only sense in which they
mattered at all was as the incidental building blocks of the history.
That's what I mean by the "shape" of the history: when merging git does
walk the commits to see how it all holds together, but git doesn't then
care in any way what the commits *do* apart from how they connected up
the history of the two branches.
And once git has found the common commit, it then just merges purely based
on the contents of the common commit and the two (or more, in the case of
octopus merges) endpoints. So again, at that point it never looks at any
of the individual commits, it only looks at what the *state* was.
(This is all a bit more complex when thers is more than one "common
commit", but that's just a detail, and doesn't change the argument).
And git-rebase is obviously totally different: git-rebase also finds the
common points, but uses that to just discard all the shared history that
cannot matter, and then it walks all the *unshared* commits to match them
up and see which ones already look like they exist (as another commit, but
one that has the equivalent diff!), and which ones are worthy of trying to
add.
> A is a commit, A* is the commit which reverts (the data change by)
> A. L and F are to mark the last and first commits before and after
> the first merge m. T is the tip of 'branch'
>
> After merge point m2, the change introduced by A will *not* be in
> master. This much makes sense.
Yes.
> What did not make sense is how Git determines to leave it out. But
> I think that after drawing the above, it's now clear:
>
> by shape you mean the actual graph, and when 'branch' is merged into
> master at m2, Git goes back in time to conclude that master...L must
> already be present in master due to the intersection of the two
> lines at m, and thus finds commit F as the "oldest direct
> descendant" of m2. L is an older descendant of m2, but it's not
> direct in the sense that there are multiple paths from m2 to L. Thus
> Git will only merge F..T at m2.
Exactly.
> Or as you put it:
>
> > If Foo has had *new* commits in the meantime, those new commits
> > will show up, of course, but the old commits have absolutely zero
> > effect, because they will be part of the common history.
>
> I think I am (moderately) clear again on the inner working of Git.
> Sorry for the confusion.
Hey, I think these things are good to clarify, maybe somebody else didn't
quite understand it. And the more "graphical" and concrete some problem
is, the more likely people are to "get it".
What is interesting is how the actual rules that git follows are *really*
simple. But they result in all this fairly complex, almost "emergent"
behaviour. It's like the basic data structures: in many ways, git really
only has those four basic object types, and you can really descibe
everything git does in terms of those very simple core data structures:
but a *repository* is certainly not something simple.
But the final "behaviour" really all comes from the interactions of
things. The rules are all really really simple, the data structures are
all totally trivial, but the possibility for interactions causes all the
excitement.
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 19:38 ` Linus Torvalds
@ 2007-10-23 19:46 ` Linus Torvalds
0 siblings, 0 replies; 16+ messages in thread
From: Linus Torvalds @ 2007-10-23 19:46 UTC (permalink / raw)
To: martin f krafft; +Cc: git discussion list
On Tue, 23 Oct 2007, Linus Torvalds wrote:
>
> > by shape you mean the actual graph, and when 'branch' is merged into
> > master at m2, Git goes back in time to conclude that master...L must
> > already be present in master due to the intersection of the two
> > lines at m, and thus finds commit F as the "oldest direct
> > descendant" of m2. L is an older descendant of m2, but it's not
> > direct in the sense that there are multiple paths from m2 to L. Thus
> > Git will only merge F..T at m2.
>
> Exactly.
Side note: strictly speaking, git will not merge "F..T" in the sense that
it never actually even _looks_ at any of the commits in that range per se.
So what it really does is to look at state of the common point ('L') and
then the states of the end-points, and merge things based purely based on
that state, and then join the histories up.
Yes, that *effectively* means merging all the changes from 'F'..'T', but I
want again to point out that the actual changes done by any of the
individual commits in that range are never even looked at. They really are
totally irrelevant on their own.
So if 'F' did a lot of changes and 'T' undid most of them, the merge
algorithm will not ever even *see* those changes. They were irrelevant.
It's not that git sees the changes and then sees that 'T' undid them: git
will literally never actually even look at them in the first place!
That's what I mean by only taking "state" into account. It didn't matter
what any individual commit did. Git won't even have looked at the commit,
other than to find its parent. When git merges, it literally looks at just
the end results, and the last common state(s).
So history matters a great deal to merging, but it only matters in the
global "shape" sense, never in the "per-commit" sense.
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 19:33 ` Junio C Hamano
@ 2007-10-23 19:49 ` Linus Torvalds
2007-10-23 20:33 ` [PATCH] revert/cherry-pick: work on merge commits as well Junio C Hamano
0 siblings, 1 reply; 16+ messages in thread
From: Linus Torvalds @ 2007-10-23 19:49 UTC (permalink / raw)
To: Junio C Hamano; +Cc: martin f krafft, git discussion list
On Tue, 23 Oct 2007, Junio C Hamano wrote:
>
> Desire to revert an octopus would, as you demonstrated, often be
> to revert only one arm, but I think allowing to revert a twohead
> merge should be trivial. If we define "reverting a merge" to
> always revert all arms, then this should suffice.
The only reason I don't like this is that it kind of assumes that the
mainline is the first parent.
Maybe I'd like to revert a merge, but I want to revert a merge that
somebody *else* did, and maybe it was the first-hand parent I don't like.
Those kinds of issues don't exist with non-merge commits: there's never
any question "which side" to revert.
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH] revert/cherry-pick: work on merge commits as well
2007-10-23 19:49 ` Linus Torvalds
@ 2007-10-23 20:33 ` Junio C Hamano
0 siblings, 0 replies; 16+ messages in thread
From: Junio C Hamano @ 2007-10-23 20:33 UTC (permalink / raw)
To: Linus Torvalds; +Cc: martin f krafft, git discussion list
Usually you cannot revert a merge because you do not know which
side of the merge should be considered the mainline (iow, what
change to reverse).
With this patch, cherry-pick and revert learn -m (--mainline)
option that lets you specify the parent number (starting from 1)
of the mainline, so that you can:
git revert -m 1 $merge
to reverse the changes introduced by the $merge commit relative
to its first parent, and:
git cherry-pick -m 2 $merge
to replay the changes introduced by the $merge commit relative
to its second parent.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
Linus Torvalds <torvalds@linux-foundation.org> writes:
> On Tue, 23 Oct 2007, Junio C Hamano wrote:
>>
>> Desire to revert an octopus would, as you demonstrated, often be
>> to revert only one arm, but I think allowing to revert a twohead
>> merge should be trivial. If we define "reverting a merge" to
>> always revert all arms, then this should suffice.
>
> The only reason I don't like this is that it kind of assumes that the
> mainline is the first parent.
>
> Maybe I'd like to revert a merge, but I want to revert a merge that
> somebody *else* did, and maybe it was the first-hand parent I don't like.
>
> Those kinds of issues don't exist with non-merge commits: there's never
> any question "which side" to revert.
Fair enough. How about this?
Documentation/git-cherry-pick.txt | 9 +++++++-
Documentation/git-revert.txt | 9 +++++++-
builtin-revert.c | 42 ++++++++++++++++++++++++++++++------
git-compat-util.h | 13 +++++++++++
4 files changed, 64 insertions(+), 9 deletions(-)
diff --git a/Documentation/git-cherry-pick.txt b/Documentation/git-cherry-pick.txt
index 76a2edf..937c4a7 100644
--- a/Documentation/git-cherry-pick.txt
+++ b/Documentation/git-cherry-pick.txt
@@ -7,7 +7,7 @@ git-cherry-pick - Apply the change introduced by an existing commit
SYNOPSIS
--------
-'git-cherry-pick' [--edit] [-n] [-x] <commit>
+'git-cherry-pick' [--edit] [-n] [-m parent-number] [-x] <commit>
DESCRIPTION
-----------
@@ -44,6 +44,13 @@ OPTIONS
described above, and `-r` was to disable it. Now the
default is not to do `-x` so this option is a no-op.
+-m parent-number|--mainline parent-number::
+ Usually you cannot revert a merge because you do not know which
+ side of the merge should be considered the mainline. This
+ option specifies the parent number (starting from 1) of
+ the mainline and allows cherry-pick to replay the change
+ relative to the specified parent.
+
-n|--no-commit::
Usually the command automatically creates a commit with
a commit log message stating which commit was
diff --git a/Documentation/git-revert.txt b/Documentation/git-revert.txt
index 69db498..3457c40 100644
--- a/Documentation/git-revert.txt
+++ b/Documentation/git-revert.txt
@@ -7,7 +7,7 @@ git-revert - Revert an existing commit
SYNOPSIS
--------
-'git-revert' [--edit | --no-edit] [-n] <commit>
+'git-revert' [--edit | --no-edit] [-n] [-m parent-number] <commit>
DESCRIPTION
-----------
@@ -27,6 +27,13 @@ OPTIONS
message prior committing the revert. This is the default if
you run the command from a terminal.
+-m parent-number|--mainline parent-number::
+ Usually you cannot revert a merge because you do not know which
+ side of the merge should be considered the mainline. This
+ option specifies the parent number (starting from 1) of
+ the mainline and allows revert to reverse the change
+ relative to the specified parent.
+
--no-edit::
With this option, `git-revert` will not start the commit
message editor.
diff --git a/builtin-revert.c b/builtin-revert.c
index a655c8e..bfed69d 100644
--- a/builtin-revert.c
+++ b/builtin-revert.c
@@ -19,9 +19,9 @@
* Copyright (c) 2005 Junio C Hamano
*/
-static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] <commit-ish>";
+static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] [-m parent-number] <commit-ish>";
-static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-r] [-x] <commit-ish>";
+static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-m parent-number] [-r] [-x] <commit-ish>";
static int edit;
static int replay;
@@ -29,6 +29,7 @@ static enum { REVERT, CHERRY_PICK } action;
static int no_commit;
static struct commit *commit;
static int needed_deref;
+static int mainline;
static const char *me;
@@ -58,6 +59,12 @@ static void parse_options(int argc, const char **argv)
else if (!strcmp(arg, "-x") || !strcmp(arg, "--i-really-want-"
"to-expose-my-private-commit-object-name"))
replay = 0;
+ else if (!strcmp(arg, "-m") || !strcmp(arg, "--mainline")) {
+ if (++i >= argc ||
+ strtol_i(argv[i], 10, &mainline) ||
+ mainline <= 0)
+ usage(usage_str);
+ }
else if (strcmp(arg, "-r"))
usage(usage_str);
}
@@ -234,7 +241,7 @@ static int merge_recursive(const char *base_sha1,
static int revert_or_cherry_pick(int argc, const char **argv)
{
unsigned char head[20];
- struct commit *base, *next;
+ struct commit *base, *next, *parent;
int i;
char *oneline, *reencoded_message = NULL;
const char *message, *encoding;
@@ -269,8 +276,29 @@ static int revert_or_cherry_pick(int argc, const char **argv)
if (!commit->parents)
die ("Cannot %s a root commit", me);
- if (commit->parents->next)
- die ("Cannot %s a multi-parent commit.", me);
+ if (commit->parents->next) {
+ /* Reverting or cherry-picking a merge commit */
+ int cnt;
+ struct commit_list *p;
+
+ if (!mainline)
+ die("Commit %s is a merge but no -m option was given.",
+ sha1_to_hex(commit->object.sha1));
+
+ for (cnt = 1, p = commit->parents;
+ cnt != mainline && p;
+ cnt++)
+ p = p->next;
+ if (cnt != mainline || !p)
+ die("Commit %s does not have parent %d",
+ sha1_to_hex(commit->object.sha1), mainline);
+ parent = p->item;
+ } else if (0 < mainline)
+ die("Mainline was specified but commit %s is not a merge.",
+ sha1_to_hex(commit->object.sha1));
+ else
+ parent = commit->parents->item;
+
if (!(message = commit->buffer))
die ("Cannot get commit message for %s",
sha1_to_hex(commit->object.sha1));
@@ -299,14 +327,14 @@ static int revert_or_cherry_pick(int argc, const char **argv)
char *oneline_body = strchr(oneline, ' ');
base = commit;
- next = commit->parents->item;
+ next = parent;
add_to_msg("Revert \"");
add_to_msg(oneline_body + 1);
add_to_msg("\"\n\nThis reverts commit ");
add_to_msg(sha1_to_hex(commit->object.sha1));
add_to_msg(".\n");
} else {
- base = commit->parents->item;
+ base = parent;
next = commit;
set_author_ident_env(message);
add_message_to_msg(message);
diff --git a/git-compat-util.h b/git-compat-util.h
index f23d934..a12c36b 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -376,4 +376,17 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
return 0;
}
+static inline int strtol_i(char const *s, int base, int *result)
+{
+ long ul;
+ char *p;
+
+ errno = 0;
+ ul = strtol(s, &p, base);
+ if (errno || *p || p == s || (int) ul != ul)
+ return -1;
+ *result = ul;
+ return 0;
+}
+
#endif
--
1.5.3.4.1324.ga7925
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-23 17:40 ` Linus Torvalds
2007-10-23 18:08 ` martin f krafft
@ 2007-10-31 21:16 ` Alejandro Martinez Ruiz
2007-10-31 21:27 ` martin f krafft
2007-10-31 21:34 ` Linus Torvalds
1 sibling, 2 replies; 16+ messages in thread
From: Alejandro Martinez Ruiz @ 2007-10-31 21:16 UTC (permalink / raw)
To: Linus Torvalds; +Cc: martin f krafft, git discussion list
On Tue 23 Oct 2007, 10:40, Linus Torvalds wrote:
>
> So a "revert" is fundamentally different from a "undo". Most of the time
>
> [cut]
>
> So sometimes the behaviour of "git revert" will be exactly what people
> expected and wanted ("good, I'll never get that commit again when I pull,
> because I told git that I don't want that commit"), and sometimes it will
> _not_ be what people expected and wanted ("oh, I didn't get that commit,
> even though I was now ready for it - because I had reverted it back when I
> was *not* ready for it").
>
> See? The logic is exactly the same in both cases, but one was good, the
> other bad, and the only difference was really the mindset of the user.
>
> A tool can't ever get "mindset of the user" differences right. At least
> not until we add the "esp option" ;)
>
> So I really don't want to push this as a problem or deficiency, I think
> it's a good thing. But it's a good thing only when people are *aware* of
> what "revert" really means.
So how about an "undo" command or a switch for revert with a special
meaning like "hey, this one is a nice commit, but it ain't ready yet,
I'd like you to ignore I ever committed the thing when merging or
rebasing again, thanks"?
Alex
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-31 21:16 ` Alejandro Martinez Ruiz
@ 2007-10-31 21:27 ` martin f krafft
2007-10-31 21:34 ` Linus Torvalds
1 sibling, 0 replies; 16+ messages in thread
From: martin f krafft @ 2007-10-31 21:27 UTC (permalink / raw)
To: git discussion list
[-- Attachment #1: Type: text/plain, Size: 960 bytes --]
also sprach Alejandro Martinez Ruiz <alex@flawedcode.org> [2007.10.31.2216 +0100]:
> So how about an "undo" command or a switch for revert with a special
> meaning like "hey, this one is a nice commit, but it ain't ready yet,
> I'd like you to ignore I ever committed the thing when merging or
> rebasing again, thanks"?
Revert does exactly that, by reverting the content. That's all Git
cares about. Does
http://lists-archives.org/git/634475-unmerging-feature-branches.html
make it clear?
--
martin | http://madduck.net/ | http://two.sentenc.es/
"getting a scsi chain working is perfectly simple if you remember that
there must be exactly three terminations: one on one end of the
cable, one on the far end, and the goat, terminated over the scsi
chain with a silver-handled knife whilst burning *black* candles."
-- anthony deboer
spamtraps: madduck.bogus@madduck.net
[-- Attachment #2: Digital signature (see http://martin-krafft.net/gpg/) --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: unmerging feature branches
2007-10-31 21:16 ` Alejandro Martinez Ruiz
2007-10-31 21:27 ` martin f krafft
@ 2007-10-31 21:34 ` Linus Torvalds
1 sibling, 0 replies; 16+ messages in thread
From: Linus Torvalds @ 2007-10-31 21:34 UTC (permalink / raw)
To: Alejandro Martinez Ruiz; +Cc: martin f krafft, git discussion list
On Wed, 31 Oct 2007, Alejandro Martinez Ruiz wrote:
>
> So how about an "undo" command or a switch for revert with a special
> meaning like "hey, this one is a nice commit, but it ain't ready yet,
> I'd like you to ignore I ever committed the thing when merging or
> rebasing again, thanks"?
There is only one undo command, and that one we've had since day 1:
git reset --hard <state-you-want-to-go-back-to>
will happily undo anything at all (including an earlier undo, apart from
uncommitted dirty tree state, which is gone, gone, gone after that
"undo" and can not be retrieved).
That's the only real true "undo" with clear semantics - it actually does
undo the whole history.
But the kind of "undo" you wish for is not really possible. It implies a
level of semantics that the system just doesn't know or care about. It
also implies that anything else than the shape of history would matter for
merging, which is just anathema to everything that makes git good in the
first place.
That said, in practice, this really seldom does come up. You can often use
"git revert" as that kind of undo, and when you later do the merge, and
the other side has fixed up the code, it's (a) likely going to be obvious
in the conflicts and (b) if the fixes were to infrastructure and you had
no conflicts, it's really easy to just revert the revert too!
Linus
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2007-10-31 21:35 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-23 15:24 unmerging feature branches martin f krafft
2007-10-23 16:19 ` Matthieu Moy
2007-10-23 16:50 ` Linus Torvalds
2007-10-23 17:16 ` martin f krafft
2007-10-23 17:40 ` Linus Torvalds
2007-10-23 18:08 ` martin f krafft
2007-10-23 18:24 ` Linus Torvalds
2007-10-23 19:17 ` martin f krafft
2007-10-23 19:38 ` Linus Torvalds
2007-10-23 19:46 ` Linus Torvalds
2007-10-31 21:16 ` Alejandro Martinez Ruiz
2007-10-31 21:27 ` martin f krafft
2007-10-31 21:34 ` Linus Torvalds
2007-10-23 19:33 ` Junio C Hamano
2007-10-23 19:49 ` Linus Torvalds
2007-10-23 20:33 ` [PATCH] revert/cherry-pick: work on merge commits as well Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).