Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I edit a commit with interactive rebase as uncommited?

Tags:

git

I would like to use an interactive rebase to edit a previous commit, but when I enter the edit mode for that commit all the files have been committed.

I know I can make changes and amend the commit but I want all the changes uncommitted initially (staged or otherwise) so I can edit it as if it was moments before it was originally committed. Is this possible?

like image 711
Elliot Chance Avatar asked Oct 23 '25 19:10

Elliot Chance


2 Answers

Imagine git rebase --interactive gives you this list:

pick 3d15626 first
pick 7911b8b second
pick 60d94b4 third

and you now want to edit the second commit in its un-committed state. For this change its line like the this:

pick 3d15626 first
exec git cherry-pick --no-commit 7911b8b && false
pick 60d94b4 third

What this line does:

  • instead of picking the commit normally git cherry-pick is executed
  • git cherry-pick will apply the "second" commit (note the same commit hash used) to the index and the working tree, preload the commit message but not commit (--no-commit)
  • && false ensures that the executed command "fails" by returning a failing exit code. This forces git rebase to break after this line to let you "fix" the problem, i.e. you can now edit the commit
  • after you are done editing you can git commit it again. The editor will automatically contain the original commit message thanks to git cherry-pick
  • after you committed the commit and the index is clean again you can continue with git rebase --continue
like image 177
acran Avatar answered Oct 26 '25 08:10

acran


You can at least reset to the previous commit, add everything and commit: you will then able to set again the commit message.

 git reset @~
 git add .
 git commit --interactive

Or actually just git commit, since you already added what you wanted.
That would still open an editor for you to enter the commit message again.

like image 31
VonC Avatar answered Oct 26 '25 07:10

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!