0

I merged a branch into another but now I want to revert the merge I just pushed to the repo so I did the following:

git revert -m 1 <log hash>

but this opens the VIM commit message editor which is giving me issues. When the VIM editor opens, it tells me to place my commit message, so what I did was

  1. Press i key to insert
  2. Wrote my commit message like this --> RM000: Reverting merge
  3. Exited out by :x

However, when I push my changes, I get a commit message regex error as we have a a convention of how commit messages should be so when I entered i in step one and wrote my commit message in step two, it didn't take my commit message correctly.


My question is, how can I do the commit message without having the VIM pop up? i.e after I use git revert -m 1 <log hash> I want to do git commit -m "RM:000 Reverted" but once I use the git revert it automatically opens the VIM editor.

Robin
  • 575
  • 2
  • 10
  • 26
  • exiting out the vim cancels the commit. you're supposed to do :wq instead – L_Church Feb 14 '18 at 13:15
  • 1
    Be adviced, reverting the merge commit does not actually revert the merge itself - only the changes that the merge brought with it. Git will still see the branch as merged, so if you want to "re-merge" the branch again later, you'll need to revert the revert-commit instead. See https://stackoverflow.com/a/1078209/1210983 – ohaal Feb 14 '18 at 13:17
  • @ohaal I don't understand :(. All I want to do is go back to the revision of how the branch looked before I merged but still keep my merge history intact. – Robin Feb 14 '18 at 13:20
  • @L_Church Thx. Will try that. I thought `:x` exists and saves for you. – Robin Feb 14 '18 at 13:21
  • @Robin I'm just warning you that if you later after reverting the commit find that you want to merge the (now reverted) branch back again. Then instead of merging, you will need to do a revert of the revert-commit you are now creating. (Then merge the branch after that to include any changes that has occured after the first time you merged.) – ohaal Feb 14 '18 at 13:25
  • @ohaal Hmm so what will be the correct best practice way of reverting a merge? – Robin Feb 14 '18 at 13:36
  • It depends on the situation and the branching strategy chosen. If you are working alone on a feature branch for instance, it would be fine to do a force push to overwrite the history of the branch. If you are working on the same branch as many others, it is fine to do a revert - but you need to be aware of the restriction I mentioned when you want to re-apply the changes from the merge. – ohaal Feb 14 '18 at 13:56
  • @L_Church: `:x` means "write out the file if necessary, then exit normally" in vim. (I use `ZZ` myself, which is the visual-mode alias for the ex-mode `:x` command. I'm not sure if vim uses the same terminology as the old `vi` editor on which vim was originally modeled, but `vi` was an extension to `ex`, which was the "ex"tended `ed` editor. `ex` had a ":" prompt so all `:...` commands in old `vi` were `ex` commands!) – torek Feb 14 '18 at 16:00
  • i noticed after googling... i am too used to `:wq` – L_Church Feb 14 '18 at 16:01

1 Answers1

3

Well... comment conversation may have presented the user with a solution, but the question wasn't answered and the conversation raises more questions, so here are some things:


First of all, if you want to enter the commit message for a revert on the command line, you can say

git revert -n -m 1 <hash>
git commit -m "RM:000 Reverted"

Adding the -n option tells revert not to automatically commit. It also means that if you revert multiple commits at once, the undo changes are combined into the index, so when you do manually commit they will all be squashed together. Maybe that's what you want, maybe not; in this case, it's just one commit, so it doesn't matter.


That said, you might want to either familiarize yourself more with the vi editor, or change your git config to use an editor with which you're more comfortable. You can set the core.editor config value or the environment variable GIT_EDITOR to specify a command for git to run when it needs to show you an editor.


Lastly, as pointed out in the comments, undoing a merge that has been pushed is a very difficult thing; every solution has negative consequences.

You can revert the commit, as you are trying to do. This is good in many ways, but it leaves git thinking that the branch has been merged and accounted for, even though its changes have been undone. So if you want it later, you have to do one of two things:

1) You can "revert the revert"

2) You can do a rebase -f on the branch before merging; this causes the branch to be rebuilt from new commits that git won't think are "already accounted for".

You can read more about these techniques in numerous articles and SO answers.

The other option is to use git reset to remove the merge from the branch history. This has all of the consequences of rebasing a branch after sharing it, so see the git rebase documentation under "recovering from upstream rebase" (or search for questions about force pushing, as many of them will spell this out).

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • Ty! this was very helpful. I will give it an upvote as soon as I get my 15 reputation points. – Robin Feb 14 '18 at 20:04