How to undo a Git commit? git reset HEAD^: Undo the last commit and restore to the state before commit. git revert HEAD: Create a new commit with the opposite content than the previous commit. git reset
: Undoes the specified commit using the commit's SHA-1 hash. Interactive Stage Area: Mark specific changes for retraction and then commit, excluding retracted changes.
How to retract a Git commit
When using the Git version control system, sometimes we commit code by mistake. Here's how to retract those commits:
1. Using thegit reset
git reset
command allows you to move the staged or the committed changes are withdrawn. To retract your last commit, use the following command:
git reset HEAD^
This will retract your last commit and restore the working directory to the state it was in before the commit.
2. Use thegit revert
git revert
command to create a new commit to undo the previous commit. To retract the most recent commit, use the following command:
git revert HEAD
This will create a new commit with the opposite contents of the previous commit.
3. Use thegit reflog
git reflog
command to display the history of all commits. You can use this to find the SHA-1 hash of the commit you want to retract. Once the hash is found, the commit can be retracted using the following command:
git reset
This will retract the specified commit and restore the working directory to the state it was in before the commit.
4. Using the interactive stage area
The interactive stage area allows you to select specific changes to undo. To use the interactive stage area, follow these steps:
git reset -i HEAD
git commit --amend
This will create a new commit containing all the changes you selected, but excluding the ones you retracted Change.
Note:
The above is the detailed content of How to undo git commit error. For more information, please follow other related articles on the PHP Chinese website!