Git discards all locally modified files (add, delete, modify)

Time:2021-1-16

1、 When git add is not used to cache code.

You can use git checkout — filepathname (for example: git checkout)– readme.md Don’t forget the “–” in the middle. If you don’t write it, it will become a branch!! )。 To discard all file modifications, you can use the GIT checkout. Command.

This command is used to discard all modifications that have not been added to the cache (GIT add command): content modification and entire file deletion. However, this command does not delete the newly created file. Because the newly created file has not been added to the GIT management system. So it’s unknown for GIT. Just delete it manually.

2、 The code has been cached using git add.

You can use git reset head filepathname (for example: git reset head) readme.md )To discard the cache of the specified file. To discard the cache, you can use git reset head. Command.

This command is used to clear git’s cache of file modifications. It is equivalent to canceling the work of GIT add command. After using this command, the local modification will not disappear, but return to the state as shown in (1). If you continue with the operation in (1), you can discard the local modification.

3、 The code has been committed with git commit.

You can use git reset — hard head ^ to go back and forth to the last commit state. This command can be used to go back to any version: git reset — hard commit

You can use the GIT log command to view the GIT submission history. The output of GIT log is as follows. Here you can see that the first line is commitid:

commit cf0d692e982d8e372a07aaa6901c395eec73e356 (HEAD -> master)
Author: toyflivver <[email protected]>
Date: Thu Sep 28 14:07:14 2017 +0800

Superfluous blank line

commit 14aa4d7ad4ac6fba59b4b8261d32e478e8cc99ff
Author: toyflivver <[email protected]>
Date: Thu Sep 28 14:06:44 2017 +0800

Normal code

commit da3a95c84b6a92934ee30b6728e258bcda75f276
Author: toyflivver <[email protected]>
Date: Thu Sep 28 13:58:12 2017 +0800

qbf

commit 267466352079296520320991a75321485224d6c6
Author: toyflivver <[email protected]>
Date: Thu Sep 28 13:40:09 2017 +0800

qbf

It can be seen that the current status is on the submission with commitid cf0d692e982d8e372a07aa6901c395eec73e356 (marked with head – > Master).

Many files have been modified locally, some of which are new. Because of development needs, none of these files are needed. If you want to discard them, you can use the following command:

Git checkout. All local modifications. None of the submitted, are returned to the original state
Git stash saves all uncommitted changes to stash. Reply with git stash pop.

Git reset -- hard hash # returns to a node without retaining the changes, and the existing changes will be lost.
Git reset -- soft hash # return to a node and keep the changes. The existing changes will be kept. In uncommitted, GIT status or git diff can be viewed.

Git clean - DF # returns to a node, (file deletion not tracked)
Git clean parameter
 -N do not actually delete, just drill, show the operation to be carried out, which files will be deleted. (you can use this command parameter first, and then decide whether to execute or not)
 -F delete file
 -I display the files to be deleted
 -D recursively delete directories and files (untracked)
 -Q only displays errors, and the successfully deleted files are not displayed

Note:

Git reset will delete the tracked file and return the committed file.
Git clean removes files that are not tracked

You can also use:

Git clean - nxdf
git checkout . && git clean -xdf

Here is the article about git discarding all local modified files (add, delete, modify). For more related git discarding local modified content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!