Reputation: 721
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..
sorry for my horrible english (corrections are welcome) :-)
Upvotes: 2
Views: 3575
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.
git tag master dev.target
Store a tag where dev should go before we move master.git checkout master
git reset --hard dev
Move master to where dev is.git checkout dev
git reset --hard dev.target
Move dev to where master was.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!
git branch -m master dev.mv
git branch -m dev master
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
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:
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
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
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