0

I wonder if that's possible at all. I committed some changes, commitA and then some more and more... commitB, commitC. Now I realized I don't want commitA in the code for the moment, they need to be reviewed first.

So I need to undo that commit and stash it for review and commiting them again. Is there a straightforward way to do this? Or should I just revert those changes manually, commit the revert, then introduce the changes again and stash them?

dabadaba
  • 9,064
  • 21
  • 85
  • 155

2 Answers2

2

If you've already shared the commit then yes, you're better off reverting it and recommitting later after review. If you haven't then you can just do an interactive rebase. Starting from a clean working directory:

git rebase -i <commitA>^

A text editor will open a file something like this:

pick <commitA> some commit message...
pick <commitB> blah blah...
pick <commitC> blah blah...

Change the "pick" by to "edit", save, and quit the editor. will be automatically checked out.

git reset HEAD^
git stash -u
git rebase --continue

The remaining commits will be replayed without .

Bear in mind if later commits rely on then you'll get merge conflicts.


As pointed out by sbat in the comments; if you do decide to revert you don't need to manually undo your changes. You can use the git revert command.

Calum Halpin
  • 1,945
  • 1
  • 10
  • 20
  • 1
    I'd probably add to the response that revert does not have to be manual (as asked in the original question). https://git-scm.com/docs/git-revert should be able to come to rescue if the commit is pushed. – sbat Oct 03 '19 at 13:18
  • After reverting, how do I get the changes back to the changelist so I can stash them? – dabadaba Oct 03 '19 at 13:59
  • I'd recommend checking out A and creating a new branch or tag on it rather than keeping it in the stash. You're less likely to lose it that way and you can review and apply/amend it later easily. – Calum Halpin Oct 03 '19 at 15:12
1

Hmm..... i had already answered something like this... suppose some-branch is pointing to commitC

git checkout commitA
git reset --soft HEAD~
git stash save 'stashing commit A'
git rebase --onto HEAD commitA some-branch # get rid of commit A
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Can you briefly explain what each command does? I get the gist of it, but how do you not lose the most recent commits after `commitA` when resetting the branch? – dabadaba Oct 03 '19 at 14:00
  • When you run the rebase, it will only apply revisions commitB and commitC (where some-branch is pointing to). CommitA will be discarded. – eftshift0 Oct 03 '19 at 14:07
  • It will contain the changes that _used to be_ commitA, in other words, the changes between commitA~ and commitA – eftshift0 Oct 03 '19 at 15:10
  • In the stash? Nope. Following my recipe, on the third command you are only stashing what is between commitA and its parent. – eftshift0 Oct 03 '19 at 18:09
  • Yep, you're quite right. I totally misunderstood what you'd done. – Calum Halpin Oct 03 '19 at 23:05