Although amending a previous commit is trivially simple and only requires you to type git commit --amend, what happens when your needs are far greater? This is when git reset steps in.
You might have multiple commits to undo, keeping the existing working files, then alter some of those files, and finally make a new commit.
In this example, the last three commits you made to fix something are looking quite dodgy, and your boss is getting antsy about how quickly you can release that bug fix. Typical.
This removes the latest three commits. It’s identical to: git reset --mixed HEAD~3.
The reset command will copy the old commit message to a file called .git/ORIG_HEAD which you can use later:
Or use this, which will wipe out all your existing working files:
Or use this, which leaves both the index and all your working files unchanged:
Now edit your files as you wish (the latest commit is currently b2819de):
This adds, one by one, the specific working files you wish to have in the new commit:
git add where-is/my-pay-rise.txt
Or use this, which adds all of your working files (it’s identical to: git add –a):
Finally, this creates a new commit and opens a text editor with your original commit message.
Edit it as you wish, close the message, and the commit will be performed: