Hi, I created a branch in a project and did a series of commits. I now discover that I really wanted to make all those changes against a completely different branch of the same project. There shouldn't be any issues as the branch introduces entirely new files (no changes to existing code). I'm struggling to see a way to do this without loosing the history. Some Google'ing suggests 'rebase --onto' but I'm struggling to see how this works or if it is appropriate. Any pointers much appreciated :) Thanks.
threads / discuss / 27521
Created branch in wrong place... how to fix?
Subject: Created branch in wrong place... how to fix?
Re: Created branch in wrong place... how to fix?
On Wed, Jun 01, 2011 at 01:47:12PM +0100, Howard Miller wrote:
Show 10 quoted lines
> Hi, > I created a branch in a project and did a series of commits. I now > discover that I really wanted to make all those changes against a > completely different branch of the same project. There shouldn't be > any issues as the branch introduces entirely new files (no changes to > existing code). > I'm struggling to see a way to do this without loosing the history. > Some Google'ing suggests 'rebase --onto' but I'm struggling to see how > this works or if it is appropriate. Any pointers much appreciated :) > Thanks.
"git rebase" is indeed what you want to do. Simply put, it takes all your commits and rebases them somewhere else; i.e., makes them sit on top of a different commit. See "git help rebase" for a more detailed explanation.
Of course, if you've already pushed and people have pulled from you, you'll have problems; see the "recovering from upstream rebase" section in "git help rebase".
You'll also want to do "git rebase -i". The "-i" option ensures that you can review what commits will be rebased, and even rearange, leave out, or edit commits.
Good luck.
-- () ascii ribbon campaign - against html e-mail /\ www.asciiribbon.org - against proprietary attachments
Re: Created branch in wrong place... how to fix?
Howard Miller <howard@e-learndesign.co.uk> writes:
Show 10 quoted lines
> I created a branch in a project and did a series of commits. I now > discover that I really wanted to make all those changes against a > completely different branch of the same project. There shouldn't be > any issues as the branch introduces entirely new files (no changes to > existing code). > > I'm struggling to see a way to do this without loosing the history. > Some Google'ing suggests 'rebase --onto' but I'm struggling to see how > this works or if it is appropriate. Any pointers much appreciated :) > Thanks.
This is decidely work for git rebase:
$ git rebase --onto <where it should be based> <where it is based> <branch>
-- Jakub Narebski Poland ShadeHawk on #git
Re: Created branch in wrong place... how to fix?
Howard Miller wrote:
Show 21 quoted lines
> > > I created a branch in a project and did a series of commits. I now > > > discover that I really wanted to make all those changes against a > > > completely different branch of the same project. There shouldn't be > > > any issues as the branch introduces entirely new files (no changes to > > > existing code). > > > > > > I'm struggling to see a way to do this without loosing the history. > > > Some Google'ing suggests 'rebase --onto' but I'm struggling to see how > > > this works or if it is appropriate. Any pointers much appreciated :) > > > Thanks. > > > > This is decidely work for git rebase: > > > > $ git rebase --onto <where it should be based> <where it is based> <branch> > > Thanks.... I've re-read the rebase help and spotted the appropriate > example. This makes sense. > > The only thing that I'm a little bit unsure of is what does 'upstream' > actually mean (used in the help quite liberally)? Does it just mean an > older ancestor of the current HEAD? I was struggling to find a definition.
gitglossary(7) has the following definition:
upstream branch::
The default branch that is merged into the branch in question
(or the branch in question is rebased onto). It is configured
via `branch.<name>.remote` and `branch.<name>.merge`. If the
upstream branch of 'A' is 'origin/B' sometimes we say
"'A' is tracking 'origin/B'"But in the case of rebase "upstream" means branch you forked from (you created branch from).
N.B. if the branch in question is not too old, you can examine its reflog ('git log -g <branch>' or 'git reflog show <branch>') for a creation event, e.g.:
2de58b3 gitweb/doc@{24}: branch: Created from origin-- Jakub Narebski Poland