AFL
AFL

Reputation: 71

Moving merged commits back to old branch, reverting master and keeping changes

I merged some commits into master, but now I need those changes on a new branch and master reverted back to what what it was pre-merge.

Here's what the commit history looks like now:

And here's what I would like it to look like:

Upvotes: 0

Views: 150

Answers (2)

Rodrigo Nonose
Rodrigo Nonose

Reputation: 46

If you don't have commits after the merge, you can simply reset the master to the commit before the merge and keep working on the previous branch (assuming it was only merged and not deleted).

If you have commits after the merge, you'll have to make a branch and, after that, revert the merge.

Upvotes: 0

Scott Sosna
Scott Sosna

Reputation: 1413

I'm not seeing the differences, but believe I understand the problem, it's something I've done before.

1) Create new branch: assuming master is current branch, create a new branch

c:\git\project> git branch my_new_branch

2) Reset the master branch. Let's assume the last 4 commits you need to back out.

c:\git\project> git reset --hard HEAD~4

3) Work on the new branch

c:\git\project> git checkout my_new_branch

The --hard causes the indexes to be reset as of the first commit you want to keep, and all modifications are lost.

A few caveats: -If you've already pushed the master changes to a remote, this doesn't affect the remote. At least at my site, I can't push the reset, you have to work with the remote's owner (or git admin) to do something similar to this. -The --hard should also clean up untracked files, at least the docs I read say so, but I'm not convinced it always occurs.

Upvotes: 1

Related Questions