How to undo the last Git commit?

by filiberto , in category: Third Party Scripts , 6 months ago

How to undo the last Git commit?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 6 months ago

@filiberto 

To undo the last Git commit, you can use the git revert or git reset command depending on the desired outcome. Here's how to do it:

  1. Using git revert:
  • Open your Git command prompt or terminal.
  • Run the command git revert HEAD to create a new commit that undoes the changes made in the previous commit. This will open a text editor to add a commit message, so you can enter a suitable message and save the changes to complete the revert.
  1. Using git reset:
  • Open your Git command prompt or terminal.
  • Run the command git reset HEAD~ to remove the last commit from the current branch and move the branch pointer to the previous commit. This will keep the changes made in the last commit as uncommitted changes in your working directory.
  • If you want to completely discard the changes from the last commit, you can add the --hard flag to the command, like git reset --hard HEAD~. This will remove the last commit and any changes made in it.


Remember that when using git reset, it's important to note that any commits you remove will be lost unless you have previously pushed them to a remote repository.