Home  >  Article  >  Development Tools  >  How to discard changes using Git

How to discard changes using Git

PHPz
PHPzOriginal
2023-04-10 09:03:3012008browse

Git is a distributed version control system that can help us manage code, team collaboration, version control, etc. In the process of using Git for development, we often encounter situations where we need to abandon modified files or folders. At this time, you need to learn how to use Git to discard changes.

1. git checkout

The first introduction is how to use the git checkout command. There are two common uses of git checkout:

  1. Discard all modifications to the local branch

If you have just made modifications to the local branch, but find that the modifications are incorrect or need to be To abandon previous modifications, you can use the following command:

git checkout .

After running the above command, all local branch modifications will be abandoned and rolled back to the latest submitted state.

  1. Abandon the modification of a certain file in the local branch

If you only need to abandon the modification of a certain file in the local branch, rather than the modification of all files, you can use The following command:

git checkout <file>

Among them, <file> represents the file path that needs to be abandoned. After running the above command, the modifications to the specified file will be abandoned and rolled back to the most recently submitted state.

2. git reset

git reset is also a relatively important command, which can help us abandon modifications and undo submissions. There are also two ways to use git reset:

  1. Abandon all modifications to the local branch

Like git checkout, git reset can also abandon all modifications to the local branch. The specific command is as follows:

git reset --hard HEAD

After running the above command, all modifications to the local branch will be abandoned and rolled back to the latest submitted state.

  1. Abandon the modification of a file in the local branch and undo the commit

If you not only want to abandon the modification of a file in the local branch, but also want to undo the commit, then You can use the following command:

git reset --hard HEAD^

Among them, HEAD^ represents rolling back to the previous submission. After running the above command, the modifications to the specified file will be discarded and the latest commit will be undone.

3. git revert

git revert is a command to undo a commit, overwriting the previous commit by creating a new commit. Different from git reset, git revert merges the previous modifications with the current modifications instead of completely abandoning the previous modifications.

If you need to abandon the previous modifications, but do not want to completely abandon the previous submission history, you can use the following command:

git revert <commit>

where <commit> represents The commit number that needs to be revoked. After running the above command, a new commit will be created overwriting the previous commit.

Summary:

The above is how git gives up modifying files. In daily development, these commands are very commonly used and can effectively improve our work efficiency. By learning these commands, I believe everyone will have a deeper understanding and use of Git.

The above is the detailed content of How to discard changes using Git. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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