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?
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:
git cherry-pick is executedgit 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 commitgit commit it again. The editor will automatically contain the original commit message thanks to git cherry-pickgit rebase --continueYou 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With