Richard February 2016

Undo a commit, then re-merge, in git?

Oops, I'd like to undo a merge.

I made a new branch, new_branch. I did some work on this branch, and (in order to have a backup on file sync) checked it in with a temporary commit.

Then I went back to the master branch, and made about 12 commits.

Then I went back to new_branch, and without thinking, ran:

git merge master

Now my history on this branch looks like this:

- master commit 1
- master commit 2
- master commit 3
...
- work in progress

I'd like to un-merge with master, undo the work in progress commit, then re-merge with master. Ultimately I'd like to end up so my history on this branch looks like this instead:

- master commit 1
- master commit 2
- master commit 3
...
- master commit 12 

and instead of a 'work in progress' commit, I have a bunch of changed files in staging.

Is this possible? If so, how?

Answers


David Deutsch February 2016

Assuming you have not pushed your merge or your "temporary commit", do the following (while on new_branch):

git reset --hard head~   #undoes the merge
git reset --soft head~   #undoes the temp commit, but leaves the commit's content in the index
git stash                #stashes your work in progress from the temp commit
git merge master
git stash pop

You may want to tag HEAD first with git tag temp_tag just in case these steps do not do exactly what you want, so you don't lose any work.

Edit: Note that these instructions apply if your description of the steps you took (temp commit first, then a merge) is correct; the branch history you provide seems to imply that the merge happened before the temp commit.


Tobiah Zarlez February 2016

If I understand this correctly, only the new_branch code has been effected? And you do not wish to keep the work-in-progress code? And you want a new_branch to be a fresh copy of the latest of master?

In that case, simply deleting the branch and recreating it might be the most simple solution...

But let's actually answer your question, in case I misunderstood or you're interested.

In general, to reset a commit you need to find the commit right before the one you'd like to undo. In this case, I assume that is master commit 12.

Then run git reset --hard master_commit_12_sha

If you don't want to have to find the sha (the series of numbers that identifies a commit), you can also count backwards from the head.

git reset --hard HEAD~1

Now, I don't think you are asking this, but if the problem was this:

  • master commit 1
  • master commit 2
  • mistake_commit
  • master commit 3 ...
  • master commit 12

In this case, you can either:

  • git rebase -i <master commit 2>^, then in the interactive rebase chose to "remove" the mistake_commit.
  • git revert mistake_commit, which will make a new commit that undoes the changes make in mistake_commit. NOTE: This option may be best if already pushed to origin, since it does not change history.

Here are some links to read up about what I just said:


JohnW_ARToolKit February 2016

To keep it simple and applicable to your situation:

<your repo root>git log

This lists your current branch from head (top of screen) to the beginning commit of the branch.

Find and copy a portion of the sha that represents where you want to have the branch go back to. (Just for safety sake, and as a good habit, cut and paste a good portion of the "git log" output to a text file as a backup.)

<your repo root>git reset --hard <paste the copied sha portion here>

Viola.

Post Status

Asked in February 2016
Viewed 3,514 times
Voted 11
Answered 3 times

Search




Leave an answer