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


当前回答

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

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

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

其他回答

国家从一个承诺过渡到新的承诺

0. last commit,i.e. HEAD commit
1. Working tree changes, file/directory deletion,adding,modification.
2. The changes are staged in index
3. Staged changes are committed

状态转换的动作

0->1: manual file/directory operation
1->2: git add .
2->3: git commit -m "xxx"

检查差异

0->1: git diff
0->2: git diff --cached
0->1, and 0->2: git diff HEAD
last last commit->last commit: git diff HEAD^ HEAD

恢复到上次提交

2->1: git reset
1->0: git checkout .     #only for tracked files/directories(actions include modifying/deleting tracked files/directories)
1->0: git clean -fdx     #only for untracked files/directories(action includes adding new files/directories)
2->1, and 1->0: git reset --hard HEAD

相当于git的克隆,没有重新下载任何东西

git reset && git checkout . && git clean -fdx

还有git stash——它可以“存储”你的本地更改,可以在以后重新应用,也可以在不再需要时删除

更多关于存储的信息

另一个选项是取消未提交的更改:

git restore <file>

丢弃工作目录中的更改。

添加这个答案是因为之前的答案会永久删除您的更改

安全的方法

Git stash -u

解释:隐藏本地更改,包括未跟踪的更改(-u标志)。该命令保存本地修改,并恢复工作目录以匹配HEAD提交。

想稍后恢复更改吗?

Git隐藏pop

解释:该命令将对当前工作树状态的顶部重新应用更改。

想要永久删除更改?

Git藏匿点

解释:该命令将永久删除所存储的条目

链接到git存储文档

对于那些到达这里搜索是否可以撤消git clean -f -d的人,通过它在eclipse中创建的文件被删除,

你也可以在UI中使用“restore from local history”为ref: restore from local history进行同样的操作