The Problem
Although amending a previous commit is can be very simple and only needs you to type git commit --amend, what happens when you have to do more? This is when git reset steps in.
You might have many commits to undo, keeping the existing working files, then you might alter some of those files, and finally make a new commit.
The Solution
In this example, the last three commits you made to fix something are looking quite wrong, and your boss is getting agitated about how swiftly you can release that bug fix. Typical.
This command removes the latest three commits, leaving the working files as they are.
It’s identical to: git reset --mixed HEAD~3. And it 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: