2

I have done by mistake the changes on the master, instead of my new branch. I want to move them on the new branch. The problem is that I have done commits, and I have not seen that it is on master. When I wanted to do push... I have seen it. Is there a way to go back and move the changes from master to the branch, without redoing all that I have done once?

Here is how it looks like:

* d4452d9 (HEAD, master) Added saving images for viewing
| * 129aa64 (branch2) Added interiors_folder in config file
| * ae892d8 Added images of interior
|/  
* c08a8d4 (origin/branch1, branch1) Before new branch
* b7b9dd5 (origin/master) Removed printing

And I want it like this:

* d4452d9 (HEAD, branch1) Added saving images for viewing
| * 129aa64 (branch2) Added interiors_folder in config file
| * ae892d8 Added images of interior
|/  
* c08a8d4 (origin/branch1, branch1) Before new branch
* b7b9dd5 (origin/master) Removed printing

1 Answer 1

2

It looks like the actual commit structure is what you want, just the branches point to the wrong commits. Ensure that there are no important changes in the working tree, and read the full answer first to check whether the changes really do what you want. Then, do this:

git checkout branch1
git reset --hard master

This will change branch1 so that it points to the same commit as master, which is where we want it. The --hard parameter tells git to also check out the working tree and index from that commit, so that we won't have uncommitted changes. That means all uncommitted changes will be overwritten. This is why I said the working tree should be freed of important changes...

Anyway, not we just have to do the same with the master branch:

git checkout master
git reset --hard origin/master

I'm assuming that this is where you want master to point now since you didn't specify it in your question. You could also just delete the local master branch and check it out again from origin/master for the same result.

Not the answer you're looking for? Browse other questions tagged or ask your own question.