Merc
Merc

Reputation: 17087

What to do after 2 commits in the wrong branch

I had a repo that has branch master, branch1 and branch2 that are all pointing to the same commit (let's call it aaaaaa1).

I wanted to checkout branch1 and make some changes. However, I forgot. Instead, I did 2 commits to master (aaaaaa2 and aaaaaa3, without pushing anything anywhere (this is only local).

What do I run so make it so that master is reset to aaaaaa1 and branch1 is set to aaaaaa3 as it should have been in the first place?

Upvotes: 2

Views: 63

Answers (2)

axiac
axiac

Reputation: 72366

If I understood your question correctly, you want to swap the branches master and branch1. You can do it in many ways. One of the easiest is to create a third (temporary) branch and use the same technique you use to swap the values of two variables (tmp = a; a = b; b = tmp).

Since you already have branch2 that points to the same commit as branch1 you can use it as the temporary branch (but do not delete it afterwards). In the variables swapping code above, branch2 is tmp, branch1 is a and master is b).

The tmp = a assignment already happened sometime in the past (branch2 already points to the same commit as branch1). All you have to do are the branch operations equivalent to the other two assignment above.

One way to change where a branch points is to delete and re-create it. Another one is to use reset --hard. The delete & re-create way has a drawback: it breaks the link of master with the branch it tracks in the remote repository.

Let's do it the easy way:

# a = b (branch1 = master)
git checkout branch1
git reset --hard master
# Here branch1 moved to master

# b = tmp (master = branch2)
git checkout master
git reset --hard branch2
# Here master moved to branch2 (where branch1 was before too)

Warning!

Before everything else, make sure you don't have uncommitted changes. Any uncommitted change will be lost on git reset --hard.

If you have changes but you cannot commit them, stash them:

git stash

After you complete the swapping you can checkout branch1 and unstash the changes to set the working tree in the same status as it was when you started the procedure:

git stash apply

Upvotes: 1

Petr Hejda
Petr Hejda

Reputation: 43591

git checkout branch1
git rebase master

Here you have branch1 pointing to aaaaaa3... Then:

git checkout master
git reset --hard aaaaaa1

And here you have master pointing back to aaaaaa1


edit:

Rebase takes commits from specified branch (in this case master) that are not in the current branch (in this case branch1) and commits them here.

Reset --hard can be used to just empty working tree as you used it alredy. Also, with commitID parameter (in this case aaaaaa1), you can reset your branch to specific commit as if the commits after that were never there.

In the question you stated you didn't push the commits to remote server, so this paragraph doesn't need to concern you now... But be careful when using reset --hard on code that you already pushed to remote server. You would have to push with -f (force) parameter because you are basically changing the branch structure.

Upvotes: 2

Related Questions