Home > Development Tools > git > body text

How to cancel file modification in git? How to cancel a submission?

PHPz
Release: 2023-04-03 09:32:06
Original
4013 people have browsed it

As a developer, using Git is essential. In Git, we often encounter situations where we need to cancel file modifications or undo commits. Although you can undo file modifications by manually changing the file or using the command line, Git provides us with some very convenient ways to handle these situations.

This article will introduce in detail how to use Git to cancel file modifications and how to undo commits.

Git cancels file modification

Situation 1: The file is not added to the temporary storage area

If you have modified a file but have not added it to Git yet Staging area, then it is very easy to cancel modifications. You can use the following command to undo file modifications:

git checkout -- <file>
Copy after login

This command will restore the file to the most recently submitted state. For example, if you are editing a file named test.txt and you have made changes to it, you can use the following command to cancel the changes:

git checkout -- test.txt
Copy after login

This will test.txt The file is restored to the state when it was last submitted.

Scenario 2: The file has been added to the staging area

If you have added the file to the Git staging area, canceling the modification is a little more troublesome. You need to use the following two commands:

git reset HEAD 
git checkout -- <file>
Copy after login

The first command will remove the file from the Git staging area, and the second command will restore the file to the most recently submitted state. For example, if you have added the test.txt file to the Git staging area and modified it, you can use the following command to cancel the modification:

git reset HEAD test.txt
git checkout -- test.txt
Copy after login

These two The order of commands is very important. If you use the git checkout -- test.txt command first, Git will restore the file to the most recently submitted state, ignoring the changes you made in the staging area.

Situation 3: The file has been submitted

If you have submitted a file to Git, then you need to use the git revert command to undo the modification. This command creates a new commit that cancels the previous commit. For example, if you submitted a file named test.txt on the master branch and modified it, you can use the following command to undo the submission:

git revert HEAD
Copy after login

This command will open an editor that allows you to enter undo information about this commit. If you want to submit directly, you can use the following command:

git revert --no-edit HEAD
Copy after login

This will directly submit the revocation.

Undo Commit

Sometimes, you realize that there is a problem with the code you submitted, or that you don’t want to add it to version control. In this case, you need to undo the commit.

Situation 1: Not pushed to the remote warehouse yet

If you have submitted the code locally but have not pushed it to the remote warehouse, you can use the following command to undo the submission:

git reset HEAD~1
Copy after login

This command will undo the most recent submission and restore the code to the state of the last submission.

Scenario 2: Already pushed to the remote warehouse

If you have pushed the code to the remote warehouse, you can use the following command to undo the submission:

git revert <commit_id>
Copy after login

Among them, <commit_id> is the identifier of the commit you want to undo. This command creates a new commit and cancels the specified commit within it.

Conclusion

Git provides a very convenient way to cancel file modifications and undo commits. You can easily handle these situations with the commands described in this article. Of course, we also need to be very careful when using these commands to avoid misoperation.

The above is the detailed content of How to cancel file modification in git? How to cancel a submission?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!