Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

git - Removing/undoing a merge on Sourcetree

I made quite few changes in my project (I was working on a remote branch and not the master), I committed them and created a pull request on BitBucket and merged the branch to master. I had forgotten to push my changes after the commit. Now, after trying switching the current branch to my remote branch and reverting to the commit before the merge, I managed to get all my changes back and back them up elsewhere in my system. What I want to do now is undo the bad merge that I did. Each time I click on the merge and select "Reverse commit", I get the following error message:

"error: Commit is a merge but no -m option was given.

fatal: revert failed"

The branches look like this now:

enter image description here

I want to remove the merge and bring it to a state such that it doesn't say master(4 behind) anymore.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This method is based on history deletion:

  1. Check out the branch you made the mistake on
  2. Right click on the commit you want to reset the branch to
  3. Click "Reset current branch to this commit"
  4. Select "Hard" mode and click "OK"
  5. Unfortunately you need terminal to do this bit. Type git push origin name_of_branch --force into terminal (you may need to enter your git repo username and password for it to accept the command)

Update: The current version of source tree supports forced pushes. That means you don't need to use the command mentioned in step 5! There is a check box visible when pushing that is labled "force". It is usually disabled.

How to enabled the force push checkbox:

  1. "Tools" (in the blue bar at the top of the screen)
  2. "Options"
  3. "Git" tab
  4. "Enable Force Push" (2nd section)
  5. "OK"

Check that checkbox when pushing to also fix the mistake on the origin server.

This is probably the easiest way to do it but since it is based on history deletion, If others are working on the project, make sure to let them all know what you are doing so you don't break anyone's git repository.

Update 2: If you use Visual Studio Code as your code editor, I highly recommend installing the "Git Graph" extension. It allows you to do practically everything that Source Tree allows you to do, but you can do it from directly inside your code editor! The steps for doing this using Git Graph are roughly the same as the steps for doing this in Source Tree.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...