Git undo method

In Git, you can use different commands to undo previous actions. Here are some common Git undo operations and how to use them:

  1. Undo modification (git checkout): If you have made a modification to a file but have not submitted it, you can use the following command to undo the modification:
luaCopy code 
git checkout -- <filename>

This will undo changes to the file and revert to the state of the most recent commit or pull.

  1. Undo temporary storage (git reset): If you have added files to the temporary storage area (using the git add command), but have not committed them, you can use the following command to remove the files from the temporary storage area:
perlCopy code 
git reset HEAD <filename>

This will unstage the file, but the modifications to the file will still be preserved.

  1. Undo commit (git revert): If you have submitted one or more commits, but want to undo the changes of a commit, you can use the git revert command. This command creates a new commit that applies the contents of the previous commit in reverse to the code.
phpCopy code 
git revert <commit ID>

The commit ID to be undone can be found with the git log command.

  1. Undo merge (git revert, git reset): If you want to undo a merge operation, you can use the git revert command to create a new commit and undo the merged changes. Another way is to use the git reset command to reset the branch pointer to the state before the merge, but this will lose the merged commit history.
phpCopy code 
git revert -m 1 <merge commit ID>

This will create a new commit, undoing the changes of the merge commit.

These are some common Git undo operations. According to the specific operation type you want to undo, select the appropriate command to undo. Before performing any undo operations, make sure you understand the implications and, if necessary, back up your code.

Guess you like

Origin blog.csdn.net/m0_67906358/article/details/131368158