0

Let's say I want to undo the last-but-one commit in my working tree only, and all my commits have already been pushed. Usually I use git revert -n <HASH> (see this answer). However, if that will cause conflicts due to changes made in the most recent commit, it will ask me to resolve the conflicts as normal when merging. Once conflicts are resolved and git add the files, I can't git revert -n --continue because I get error fatal: revert: --no-commit cannot be used with --continue. So instead just use git revert --continue but this causes a commit, even though I originally specified the -n option before committing, it's now ignored after the merge.

How do I avoid the commit being done after merge is completed?

Adam Burley
  • 5,551
  • 4
  • 51
  • 72
  • 3
    What's the end result you're trying to get to? If it's just to have the revert changes in your worktree, why not just let it make the commit then soft reset it? – jonrsharpe Mar 17 '23 at 12:18
  • 1
    "Let's say I want to undo the last-but-one commit in my working tree only. Usually I use `git revert -n `" Sounds like a misuse of revert. If you have not pushed, interactive rebase is a much better way. Just rebase interactive and drop the undesired commit. – matt Mar 17 '23 at 12:25
  • @jonrsharpe yes that's what I eventually did, made the commit and then immediately reset it, I'm not sure if there's a better way – Adam Burley Mar 17 '23 at 12:57
  • @matt I had already pushed in this case. updated the question. – Adam Burley Mar 17 '23 at 12:58
  • OK but now I fail to understand your objection to the commit. I mean, who really cares. The more commits the better. You can always "clean up" before the next push. You have been asked to explain your goal / motivation here, and you haven't done it. – matt Mar 17 '23 at 13:35
  • What did you expect `git revert -n --continue` to achieve? I still don't get that. If you want the changes without commiting, after your resolution you're already at the goal. Aren't you? – Romain Valeri Mar 17 '23 at 13:53
  • @matt I don't understand what more explanation of my goal is needed beyond what I have put in my question "I want to undo the last-but-one commit in my working tree only". I don't like to commit things which I'm not going to push to remote. I want to keep my branch in a state that I can push it if I need to, for example I have to leave my desk for some time due to illness or whatever. – Adam Burley Mar 17 '23 at 22:45
  • @RomainValeri yes technically you are right but I don't want the branch to be in "merging mode" (having a partially completed revert operation) forever. maybe I'm wrong to be worried about that but when I'm in a merge conflict resolution I normally see that as temporary and try to resolve it asap. to answer your initial question, I expected `git revert -n --continue` to just return my branch to the state it would have been in if I had done `git revert -n` without any merge conflicts. – Adam Burley Mar 17 '23 at 22:49
  • " I don't like to commit things which I'm not going to push to remote. I want to keep my branch in a state that I can push it if I need to" And the state after the revert, including the revert commit, is such a state. _Any state is._ Commit and push early and often! Not every commit needs it embody some perfect state. In my branches, commit includes tests that pass (or even compile) until the last one. So what? That's the history of what I did. – matt Mar 17 '23 at 22:58
  • Also what if you needed to revert the revert? You can't do that unless the revert is a commit. – matt Mar 17 '23 at 22:59
  • @AdamBurley Oh! I get it, you just want to "end" the revert sequencer but keep your changes as they are. I'm writing an answer, there's an option for this :-) – Romain Valeri Mar 17 '23 at 23:45

2 Answers2

1

To exit the sequencer after your resolution and keep your pending changes, but without commiting, you have the --quit option for that very purpose.

git revert --quit

An alternative could be to just git reset, it would unstage your changes and end the sequencer in the same go.

See in the doc.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
0

--continue (in git rebase, git merge, git cherry-pick, etc) does not accept other parameters. git revert --continue will wrap up knowing that you used -n to fire it in the first place.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • it didn't though, it still made the commit – Adam Burley Mar 17 '23 at 12:57
  • Of course it made the commit. It won't remove the original commit from history. What did you expect? – eftshift0 Mar 17 '23 at 13:06
  • Oh.... I just understood what you _want_ from `-n`. `-n` only tells git _hey, hold the commit for a little bit as I intend to do stuff_... but you will have to wrap it up with `git commit` or `git revert --continue` at some point.... which will create a commit. – eftshift0 Mar 17 '23 at 13:08