Home > database >  How do I do an interactive git amend?
How do I do an interactive git amend?

Time:07-30

How can I undo parts of the last commit, while selecting which parts using an interactive patch mode? This would come in useful when you've accidentally checked in a few lines too many in your last commit, e.g. debug print statements or whitespace changes that your editor did for you.

The naive approach is

git commit --amend --interactive --patch

but this does not seem to work. i.e. it just drops you into an editor to do a rewording.

The ugly approach is

git show HEAD~1:path/to/some/file > path/to-some/file
git add -up
git commit --amend

but this is annoying because it makes you specify the file beforehand, and makes you re-add all patches one by one (rather than allowing you to un-add only bits and pieces)

CodePudding user response:

What about

git reset -p HEAD~
git commit --amend --no-edit

First command will allow you to unstage chunks interactively (select y for the parts you want to get out of the commit), then the commit amend will write that into the new commit.

CodePudding user response:

git gui offers a gui which looks a bit dry (it's stock Tcl/Tk) but it does a pretty good job at allowing you to view and edit what is about to be committed.

For your use case :

  • start git gui from the command line
  • check the "Amend Last Commit" checkbox (to the right, over the bottom text area where you can input the commit message)
  • click in files in the left hand "Staged Changes (Will Commit)" panel to view their content
  • in the "file content" pane (right hand side), you can right click to unstage individual lines, or complete diff chunks

CodePudding user response:

This is what git reset is good for. You can

git reset HEAD^
git add --interactive
git commit --amend

to "un-stage" the last commit, re-add the parts you want, and then rewrite the commit (leaving the other changes un-staged, for you to work on or discard).

You can also use git reset -p to choose hunks to un-stage, in a way that's just like (but opposite) git add -p.

  •  Tags:  
  • git
  • Related