milushov
milushov

Reputation: 721

How to perform a rebase onto a specific commit?

Gitlovers, I have one issue.

Several commits ago, after last merging dev branch into master, I forgot to switch back to the dev branch. And I continued to make commits into the wrong branch: master - which should be back to where the merge happened. How do I swap the branches so it makes the situation as if I switched back to the dev branch after I merged it into master?

I tried with rebase:

git checkout master; git rebase dev; and git checkout master; git rebase --onto dev; doing something weird for me..

enter image description here

sorry for my horrible english (corrections are welcome) :-)

Upvotes: 2

Views: 3575

Answers (4)

Schwern
Schwern

Reputation: 164679

No need to rebase or merge. Branches are nothing more than labels on commits. You can move the labels using git reset. Despite its name, reset is really the move labels around command.

  1. git tag master dev.target Store a tag where dev should go before we move master.
  2. git checkout master
  3. git reset --hard dev Move master to where dev is.
  4. git checkout dev
  5. git reset --hard dev.target Move dev to where master was.
  6. git tag -d dev.target Remove the placeholder.

You can do it without the tag using the commit id, but the tag is a big of insurance in case you screw things up. This is essentially the "undo a commit, making it a topic branch" recipe from the git-reset man page, except your topic branch already exists.

Alternatively, since you just want to swap the branches, you can rename them!

  1. git branch -m master dev.mv
  2. git branch -m dev master
  3. git branch -m dev.mv dev

The down side is if they have any upstream tracking it will be incorrect. You'd have to go into .git/config to correct it.

Upvotes: 1

Adam Dymitruk
Adam Dymitruk

Reputation: 129526

A general solution that will also work for more complex scenarios where history isn't linear. Ensure that you are on the master branch, then:

git rebase --preserve-merges --onto master^2 master dev

What must have happened for you to get in the situation is that you did this:

  1. checked out master
  2. merged dev
  3. checked out dev
  4. merged master (!!!)
  5. continued to develop on dev

the mistake is step 4. This is called a back merge and should be avoided - although some trunk based workflows benefit from it.

Upvotes: 4

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72745

You're basically moving a bunch of commits from the master branch back onto the dev branch. I had a similar situation trying to move commits from one topic branch to another. The --onto switch of the rebase command does this. I don't want to repeat my whole blog post here but this is where I detailed what I did - http://nibrahim.net.in/2012/01/09/moving_topic_branches_in_git.html

Upvotes: 2

xdazz
xdazz

Reputation: 160833

You could try:

$ git checkout dev
$ git merge master
$ get checkout master
$ get reset --hard [the revision you want to return]

Upvotes: 1

Related Questions