Update 1 (no need to investigate the parents)
The use case I need this for is when a feature branch has been merged to master, and after that something arises that makes us want to remove that feature from the master branch again. In the text below I describe how to find the parents of a merge commit, but since the checked out branch always will become the parent number one when merging in another branch, the investigation of the parents isn't needed. The only command needed is therefore
git revert the_hash_of_the_commit_to_revert -m 1
git revert the_hash_of_the_commit_to_revert -m 1
Update 2 (commands for the commit message editor)
When the commit message editor opens up, just accept and exit by writing
:q
Or, if you want to change the commit message, press i, write your message and then
<Esc> :wq <Enter>
Revert a "normal" commit
Normally when you want to undo the changes of a commit that has been pushed, you can do a reverse commit inside SourceTree by doing a right click on the commit and choose "Reverse commit..." like in the image below.But if the commit you're trying to reverse is a merge commit you'll get the message:
git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=manager-st revert --no-edit 5a1f18d0d344e4ce9f23b02752cf4674c973cdf4
error: Commit 5a1f18d0d344e4ce9f23b02752cf4674c973cdf4 is a merge but no -m option was given.
fatal: revert failed
Completed with errors, see above.
Which means you've got to use the Terminal window.
Revert a merge commit
The case
Suppose we have the state as seen in the image below. Suddenly we realize that the changes made in the demo_branch are wrong and have to be removed from the master branch.
To do this we have to issue the revert command and specify the -m option. The -m option specifies which of the parents that was the mainline that we want the resulting code to be like.
Identify the commit to revert
The commit we want to undo is the commit with id 0c9c102
Find the correct parent
To the right in the button bar in the top of SourceTree window there is a button for opening the Terminal window.
Click on it and you'll see the Terminal window below.
Run the command git log to list the commits. Find the commit we want to revert (when you´ve found your commit, stop the listing with ctrl-z). In the image below we see that commit 0c9c102 is a merge, having two parents with the ids 618383a (parent 1) and b8a9ad7 (parent 2).
In this case we want to use the commit in the master branch as parent, the commit with id 618383a that is. That parent was listed in the log as the first parent, therefore we refer to it as parent 1.
Execute the revert command
To recap, we want to revert the commit 0c9c102 using parent 1 as the index to the -m (mainline) option. Therefore the command to write in the terminal window becomes:
git revert 0c9c102 -m 1
After issuing the command an editor for editing the commit message opens up (see below). To accept the default message and exit the editor, press Esc and then ZZ.
Now a new commit has been added, which reverts the changes introduced by the merge commit.
Done!
Sources
In case you had a hard time following my explanation, maybe the answers in the sources I used can help you.
Inga kommentarer:
Skicka en kommentar