How to redo the previous git commit?

Redo the previous git commit

In this post, I provide the necessary commands to revise or redo the previous git commit. There are cases when you make an incomplete commit, you miss some files, or you would like to change the content of some files in the last commit. With the following command, you can reset your last commit:

git reset --soft HEAD~1
  • --soft Does not touch the index file or the working tree at all. This leaves all your changed files “Changes to be committed”, as git status would put it. [source: man git-branch]

After that, you can further change the content of the file or stage new files. When you are ready, you can stage the changes and craft a new commit.

Update the changes in the remote repository

If you pushed your commit to the remote repository before the redo, you must force-push the revised commit. This can be done as follows:

git push --force origin <branch_name>

Demonstration

The video below demonstrates the described commands above:

Leave a Reply