Kludge Kludge - 7 days ago 5
Git Question

Attempt to add changes to previous commit using git rebase -i fails

I'm trying to amend changes to previous commit, as described here:
How to modify a specified commit in git?

So I have my changes in 'changes to be comitted' state, and I want to 'amend' them to a previous commit, so I go

git rebase -i HEAD~3


but getting the following error:

Cannot rebase: Your index contains uncommitted changes.
Please commit or stash them.


I can't understand what Git is complaining about, since this is the state which the changes are supposed to be in, right?

What am I doing wrong?

I'm using v1.9.3

Answer

Well as it says

Cannot rebase: Your index contains uncommitted changes. Please commit or stash them.

But probably you want to add those changes to a previous commit? if so do like so:

git add .
git commit -m 'ready to rebase'

and then try again

git rebase -i HEAD~4

Then while rebase you can use the ready to rebase commit to meld into previous one.

Say you have those for commits something like

pick 932d6c5 commit one
pick 3396a30 commit two
pick 3396a30 commit three
pick 3396a30 commit ready to rebase

then change that to

pick 932d6c5 commit one
pick 3396a30 commit two
f 3396a30 commit ready to rebase
pick 3396a30 commit three

will result in

pick 932d6c5 commit one
pick 3396a30 commit two ## this will include "commit ready to rebase"
pick 3396a30 commit three

meaning of option f is

s, squash = use commit, but meld into previous commit

f, fixup = like "squash", but discard this commit's log message