我试图撤销自上次提交以来的所有更改。在看完这篇文章后,我尝试了git reset -hard和git reset -hard HEAD。我回复头部现在在18c3773…但当我查看我的本地资源时,所有的文件都还在那里。我错过了什么?


当前回答

This will unstage all files you might have staged with git add: git reset This will revert all local uncommitted changes (should be executed in repo root): git checkout . You can also revert uncommitted changes only to particular file or directory: git checkout [some_dir|file.txt] Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory): git reset --hard HEAD This will remove all local untracked files, so only git tracked files remain: git clean -fdx WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted.


总结一下:执行下面的命令基本上相当于从原始源代码复制一个新的git(但它不会重新下载任何东西,所以更快):

git reset
git checkout .
git clean -fdx

典型的使用是在构建脚本中,当你必须确保你的树是绝对干净的——没有任何修改或本地创建的目标文件或构建工件,并且你想让它工作得非常快,而不是每次都重新克隆整个存储库。

其他回答

使用以下命令

git remove-changes

git clean -fdx 

如果你想“撤销”所有未提交的更改,只需运行:

git stash
git stash drop

如果您有任何未跟踪的文件(通过运行git状态检查),可以通过运行以下命令删除:

git clean -fdx

Git stash创建一个新的stash,它将成为stash@{0}。如果你想先检查一下,你可以运行git stash list来查看你的存储列表。它看起来像这样:

stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes

每个存储都以前一个提交消息命名。

Git中有三个选项可以帮助撤消本地更改。

要查看在你的工作目录中所做的更改,你应该运行git status:

git status

用git stash撤销更改 要丢弃所有本地更改,但也要保存它们以供以后使用,您可以运行git stash命令:

git stash

用git签出撤消更改 要永久丢弃对文件的本地更改,您可以运行:

git checkout -- <file>

用git重置撤销更改 要永久丢弃对所有文件的所有本地更改,您可以执行:

git reset --hard

来源:https://www.w3docs.com/snippets/git/how-to-discard-unstaged-changes.html

如果你想“撤销”所有未提交的更改或本地更改,只需运行:

git add . 
git stash 
git stash drop
git clean -fdx

This will unstage all files you might have staged with git add: git reset This will revert all local uncommitted changes (should be executed in repo root): git checkout . You can also revert uncommitted changes only to particular file or directory: git checkout [some_dir|file.txt] Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory): git reset --hard HEAD This will remove all local untracked files, so only git tracked files remain: git clean -fdx WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted.


总结一下:执行下面的命令基本上相当于从原始源代码复制一个新的git(但它不会重新下载任何东西,所以更快):

git reset
git checkout .
git clean -fdx

典型的使用是在构建脚本中,当你必须确保你的树是绝对干净的——没有任何修改或本地创建的目标文件或构建工件,并且你想让它工作得非常快,而不是每次都重新克隆整个存储库。