0

I had changed a lot of files and added them in the Git staging area. Then, before committing them, I realised that I had to change the commit message of the previous commit. So, I did a git commit --amend and changed the commit message without going through the change log. Well, that added the files in the staging area to the previous commit too.

What should I do so as to make the previous commit in its original form and get my changed files back in the staging area so I could commit them separately ?

For what it's worth, I realise that I should have committed my changes first and then changed the commit message history using git rebase -i.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
darkryder
  • 792
  • 1
  • 8
  • 25

2 Answers2

3

You can find the SHA1 of the previous state of your previous commit using

git reflog

Using this hash, you can revert the current branch to it's previous state using

git reset --soft <SHA1>

After this, you can stash your changes, and then do git commit --amend to change the commit message.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Thanks :) Someday I'm gonna sit down and take out a couple of days and read everything about git. Someday. – darkryder Jan 22 '15 at 14:45
1

Based on the values of the commit id's in the reflog, do the following.

git reflog -5 #I assume this will be good enough; increase the value as needed
git reset --hard <commit-with-bad-message>
git commit --amend -m '<New message>'
git read-tree --reset -u <commit-that-had-extra-files>

You should now be where you wanted to be previously. Continue with your work.

Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40