git revert alone won't work. Apparently, -m must be specified.

 

You are correct. When using git revert to undo a merge commit, you often need to specify the parent number using the -m option to indicate which parent of the merge commit you want to revert. This is because a merge commit has multiple parent commits, and Git needs to know which parent's changes you want to undo. The parent numbers are 1-based, where the first parent is usually the branch you were on when you merged.

Here's an example to illustrate this scenario:

Suppose you have the following commit history:

sql
* d1a2b3f (HEAD -> master) Merge branch 'feature' |\ | * 9c8b7a6 (feature) Add feature file * | 6b5a4c3 Update file A |/ * 3a2b1c0 Initial commit

You want to undo the changes introduced by the merge commit d1a2b3f which merged the feature branch into the master branch.

To revert the merge commit, you would use the following command:

sh
git revert -m 1 d1a2b3f

Explanation:

  • -m 1 specifies that you want to revert changes from the first parent of the merge commit d1a2b3f. The first parent is the master branch, which is usually the branch you were on when you performed the merge.
  • d1a2b3f is the hash of the merge commit you want to revert.

After running this command, Git will create a new commit that undoes the changes introduced by the merge commit. The history will look something like this:

sql
* d1a2b3f (HEAD -> master) Merge branch 'feature' |\ | * 9c8b7a6 (feature) Add feature file * | 6b5a4c3 Update file A |/ * 3a2b1c0 Initial commit | * f0e1d2c Revert "Merge branch 'feature'" |/ * d1a2b3f Merge branch 'feature' |\ | * 9c8b7a6 (feature) Add feature file * | 6b5a4c3 Update file A |/ * 3a2b1c0 Initial commit

A new commit with the message "Revert 'Merge branch 'feature''" is added, and the changes from the merge commit are effectively undone.

Remember to review the changes before committing to ensure that the intended changes are being reverted.

Comments