jamesmortensen
jamesmortensen

Reputation: 34048

How can I undo a git merge, make changes, then redo the merge?

I'm working on branch myDev. I merged in some changes from the master branch last night and while I was doing this, I made the stupid mistake of also running a JavaScript formatter on a few of the files.

Today, I'm merging with a colleague's branch, and now there are dozens of conflicts in a pretty complicated part of the system, and I'm not sure what part of the diffs are from the formatter and what part is actual changes and bugfixes.

After diving down this rabbit hole, my plan is to try to undo the merge I did from master to my branch, roll back the changes from the formatter, then do the merge again.

However, I'm concerned that Git may not recognize an undone merge and let me try again. I don't want to make this problem any worse, so I'm wondering how I can undo the merge, revert the formatter changes, then merge again so that the only conflicts I see will be from real, actual changes to the logic.

Here's a screenshot to demonstrate what I'm trying to do. Since this is a sensitive operation, I want to have a plan before I start undoing stuff.

Screenshot of my commit history, with annotations outlining what I want to do

To be clear, I'm the only one working on the myDev branch. So this should mean whatever operations I do won't affect others on the team.

I was thinking of just outright deleting the last two commits from history entirely, since I am the only person working on this branch, but I'm not sure what kind of side effects that might produce.

Upvotes: 3

Views: 2891

Answers (2)

Rad Apdal
Rad Apdal

Reputation: 462

Almost the same issue with me. I have to merge a branch from remote, add my changes, then not include the changes from the merge in the push. So I did:

On local-branch-merged

$git pull origin branch

add my changes, then

$git commit mychanges

On local-branch-unmerged

$git cherry-pick sha1ofmychanges
$git push origin local-branch-unmerged

Then I just made a pull request from my origin branch to upstream.

Upvotes: 0

jamesmortensen
jamesmortensen

Reputation: 34048

Instead of going through the complicated and risky process of deleting commit history and rolling back commits, just simply create another branch, using a previous commit as the branch point.

In this case, it sounds like you want to ignore the last two commits and start at the third. Thus, here is what you should do:

1. Checkout the third commit using the SHA1 hash.

Note: replace example hash with your third commit hash

$ git checkout c21f969b5f03d33d43e04f8f136e7682  

This checks out the code at this point in time.


2. Next, create a new branch based on this commit.

$ git checkout -b myNewDevBranch

This will create a new branch from that point in time. The system now knows nothing about your botched merge with the master branch.


3. Merge from master to myNewDevBranch

Now you can try the merge from master to myNewDevBranch. This assumes you are still on the myNewDevBranch. Use git status to confirm. Afterwards, run this command:

$ git merge master

Resolve any conflicts that may occur and commit/push the changes to the server.


4. Finally, merge with myColleagueBranch

Now you're at the point you were before your failed merge with your colleague. Simply merge as you did before:

$ git checkout myColleagueBranch
$ git pull origin myColleagueBranch
$ git merge myNewDevBranch

Resolve any conflicts, and check to make sure your merge is successful. Since this now no longer contains the changes from running your JavaScript formatter, the merge should have a lot less conflicts.

Upvotes: 2

Related Questions