Amending the last commit

Suppose we make a commit and then realize that we made a mistake. If we haven't yet pushed that commit onto a remote repository, we can make the new changes and then run

git commit --amend

Git will open VS Code and we can write a new message or keep the original one. If we already know that we want to change the message, we can run

git commit --amend -m "New commit message"

Keep in mind that we are not really changing the last commit, we are creating a new one and dropping the last one. So don't amend commits that you have already pushed onto a remote repository.

Amending the last commit 1
Amending the last commit 2

Amending also works if we forgot to include a file in the last commit. Just stage the changes and then run

git commit --amend

Now the last commit also includes the new file.

If, on the other hand, we included a file that we shouldn't have, then we can use a --mixed reset. This will undo the last commit, and place all changes in our working directory. We can now choose which files we want to include and commit again.

Amending the last commit 3