git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Mathieu Malaterre <mathieu.malaterre@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: duplicate commits after git rebase -i HEAD~10
Date: Fri, 3 Dec 2010 10:34:35 -0500	[thread overview]
Message-ID: <20101203153435.GA6069@sigill.intra.peff.net> (raw)
In-Reply-To: <AANLkTi=W9MyCuOuk49AtCxR7nTa5xkddY_2HkhOvQip-@mail.gmail.com>

On Fri, Dec 03, 2010 at 04:04:53PM +0100, Mathieu Malaterre wrote:

>   I am trying to understand what I did wrong in my git rebase and
> eventually I would like to repair my mistakes. Basically after a git
> rebase, I ended up with duplicate *identical* commits. One was on the
> master branch and one was on a local branch which was merge to master.

They're not quite identical. They may make the same logical change, but
they are based on different history.

The mistake you made is to rebase commits that have been published on
another branch. Generally you would throw away the "originals" after
rebasing, but in this case, the master branch already knows about them
(due to the merge), so that may not be an option.

How you proceed with fixing it depends on what your history looks like,
and how much of it you are willing to rewrite. From your description,
your history looks something like:

             F----G <-- original branch
            /      \
  ...A--B--C--D--E--M <-- master
            \
             F'--G' <-- rebased branch

where F' is the commit matching F, and so forth. Due to merge M, master
contains the commits from the original branch, but your newly rebased
branch has the matching ones.

If you have not actually published the merged value of master to anyone
else, and you have not built anything on top of master, then it is safe
to redo that merge, like:

  git checkout master
  git reset --hard HEAD^
  git merge your-branch

If you have built on top of master, you can still redo the merge, but
you would then need to replay those new commits on top of the new merge:

 # make a new branch at master just before the merge; you will have to
 # find the commit id of the merge via "git log" or gitk
 git checkout tmp-branch M^

 # now redo the merge, using the rebased commits
 git merge your-branch

 # and now we rebase master, using just the commits from _after_ the
 # merge, on top of our re-done merge
 git rebase --onto tmp-branch M master

 # the rebase command leaves us on "master" as a side effect; we can now
 # delete tmp-branch
 git branch -d tmp-branch

If you have published your merged master for others to build on top of,
then you probably don't want to rewrite it, as it will cause pain for
others. Just live with the commits you've published, and redo your
changes as new commits on top. To restore the state of your-branch to
what it was before the rebase, look in the reflog ("git log -g
your-branch"), then reset it ("git checkout your-branch; git reset
--hard your-branch@{1}" (or whatever reflog entry looks to be correct)).

You may want to also look at this thread, which shows a similar problem,
and in which I drew a lot more pictures, which may help explain:

  http://article.gmane.org/gmane.comp.version-control.git/161823

-Peff

  reply	other threads:[~2010-12-03 15:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-03 15:04 duplicate commits after git rebase -i HEAD~10 Mathieu Malaterre
2010-12-03 15:34 ` Jeff King [this message]
2010-12-14 16:07   ` Gavin Guo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101203153435.GA6069@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=mathieu.malaterre@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).