If you have accidentally committed the wrong files into Git, but haven’t yet pushed it to the server, you can recover, or undo your commit as follows:

How to Undo a Commit and Redo it

You have just committed a file, and find yourself here;

We will call this “your accident location”.

Step 1

git commit -m "This is what you've just done"

Step 2

Start by resetting:

git reset HEAD~

Step 3

Now you can edit your files as required.

Step 4

When you are done, run the following commands:

git add .
git commit -c ORIG_HEAD

A note on Step 2

Step 2 said to use git reset HEAD~

Note that HEAD~ is the same as HEAD~1, which can allow you to undo a certain amount of commits. As we only stipulated 1 or simply ~, it only undid 1 place.

Swap this out for a greater number if you want to undo a larger amount of commits.