ChatGPT解决这个技术问题 Extra ChatGPT

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:

https://i.stack.imgur.com/bJ2eK.png

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

Are you still having issues with this or did you figure it out?
Update: this is still an issue. There is a feature request for this which is still unresolved, see jira.atlassian.com/browse/SRCTREEWIN-1832

D
Daniel Tonon

This method is based on history deletion:

Check out the branch you made the mistake on Right click on the commit you want to reset the branch to Click "Reset current branch to this commit" Select "Hard" mode and click "OK" 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:

"Tools" (in the blue bar at the top of the screen) "Options" "Git" tab "Enable Force Push" (2nd section) "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.


If only Sourcetree would come up with a warning message when trying to push to origin while behind. If the warning had the option to force push rather than just have it error on you, then the whole process could be done through the sourcetree GUI with no terminal commands necessary.
After all this I had to click "Push" on the GUI for it to actually work.
That is what step 5 is for. Clicking "Push" on it's own in Source Tree causes an error. If you are using a different GUI then yes, clicking the "Push" button might be all you have to do at the end depending on the GUI.
Thanks for updating this answer frequently, it's still solving this situtation in 2021
C
Community

It's very unfortunate that SourceTree doesn't make it easy for you to revert merge commits (at least in Windows SourceTree 1.5.2.0). However, reverting the merge can easily be accomplished from the command line, but do you want to revert the merge by adding another commit that is the reverse of the results of the merge commit, or do you want to just remove the merge commit from history altogether?

If you're not sharing your master branch with other people, the simplest thing to do would be to simply do a hard reset to remove the merge commit from history. However, if other people already have a copy of that merge commit, then you'll create extra work for them as they try to re-sync their copies of master with the rewritten version of yours.

So you need to figure out which option you want to use. I will give the steps for both, using the command line:

Remove Commit from History

git checkout master
git push origin master --force

That will overwrite the merge commit on origin/master with the current state of your local master branch, thus removing the merge commit.

Revert Commit with another Reverse Commit

git checkout master
git merge origin/master
git revert -m 1 HEAD

That will bring your local master branch in-sync with origin/master, and then you can add a reverse commit by telling git revert that the 1st parent is the one that should be considered the "mainline" parent, and it will create the reverse commit relative to the changes brought in from the other parent commit.

If later on you decide that you want to bring in the changes from the other parent again, then you'll need to add another reversion commit that reverts the reversion commit that you just made (a technique known as "reverting the revert").

See also:

Undo a Git merge?. How do I “un-revert” a reverted Git commit?. git-revert(1) Manual Page. How to revert a faulty merge.


A
Abhi

Follow this step by step:

REVERTING A MERGE COMMIT

Master was merged and pushed into Develop with conflicts/unwanted codes.

To revert using Sourcetree:

Select Develop, Select the last commit to which branch has to be reverted. Right click on that branch and select “Reset develop to this commit”. From the pop up select hard from drop down at the left bottom. Its done.

There should be no pull thereafter. If any pull occurs revert using terminal as below.

Now Merge master into develop.

To revert using Terminal:

Select Develop, Select the last commit to which branch has to be reverted. Open terminal from the Sourcetree type git reset --hard If any pull count appear in the sourcetree then type git push -f, and its done.

Merge master into develop.


F
Fernando Bonet

I found another way without command lines, a bit ugly but does the job.

So, do a hard reset to the commit you want to roll back, go to the project folder (Using Finder on Mac or Explorer on Windows) and make a copy of the whole folder. What you have inside this 'Copy' folder is the point you want to be at the end of this process.

Well... go back to source tree and then checkout the Head (the latest commit into your remote), then navigate again to the project folder, be sure you can see hidden folders because you must be able to see a folder called ".git"

https://i.stack.imgur.com/qLaNm.png

Delete everything BUT ".git" from your current project, it means your current project has nothing inside but the folder called ".git", then navigate to your 'Copy' folder and copy everything but ".git" folder and paste the content inside your current project (the one with ".git" folder only)

Done. Go to source tree and commit the changes, your project is exactly where you wanted and all changes removed.

The end.

Obs1: Delete the "Copy" folder now to clean your pc from dirty files.

Obs2: This process don't remove your changes from Git, the commits will be there, what you are doing is deleting your changes and committing it.


D
Daniel Tonon

Non-destructive method

I ran into a situation where my previous solution wasn't an option. I couldn't force push to master and and even if I could, it would have been too disruptive for the other devs.

I discovered this new method that leaves the git history 100% in tact and is completely non-disruptive for the other devs on the team.

Create a new branch just before the bad merge Cherry-pick the bad merge commit onto this new branch (this converts the merge into a normal commit) Revert the cherry-pick commit Merge the new branch into master

That's it!

(You can do most of these actions by right clicking on commits in the git history and selecting options from the context sensitive menu)

Update: Step 4 isn't showing any changes?

If this happens:

If you can commit directly to master, cherry pick the revert commit directly onto master.

If you can't commit directly to master, create a new branch off of master and cherry pick the revert commit onto that branch instead, then merge that new branch into master.