Skip to main content

Command Palette

Search for a command to run...

Git Cheat Sheet : Remove previous commit

Updated
2 min read
Git Cheat Sheet : Remove previous commit

Git is a powerful version control system that allows developers to manage their code efficiently.

Remove local committed changes

If you have accidentally made a wrong commit in Git on you local repository (not pushed to the remote). You have used git commit command, meaning the commit only exist only locally.

You can use git status to verify the last commit on your branch.

Use the following command to undo you commit:

git reset --soft HEAD~1

The command will undo (remove) the last commit on your branch, but it will keep the changes.

Remove remote committed changes

Sadly your faulty commit was pushed to the remote and you want to undo it.

⚠️ I recommend to not undo commit on the remote as you will rewrite the branch history. Instead use the revert command.⚠️

First identity the commit hash with the command below :

git log --onleline

Then you can undo the faulty commit using the hash and push the modification to the remote

git revert <commit-hash> --no-edit # Revert the commit
git push # Push the modification to the remote

The branch history will show the commit and the revert commit.

Conclusion

There are several ways to remove commits from Git, we exposed two simple methods to perform this task.

  • git reset : This command is used to undo a commit that has not been pushed to the remote, allowing you to update your local repository.

  • git revert : This command safely undoes a commit that has already been pushed to the remote by creating a new commit that revert the changes

More from this blog

K

kzankpe

13 posts