我试图撤销自上次提交以来的所有更改。在看完这篇文章后,我尝试了git reset -hard和git reset -hard HEAD。我回复头部现在在18c3773…但当我查看我的本地资源时,所有的文件都还在那里。我错过了什么?
当前回答
国家从一个承诺过渡到新的承诺
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 clean -f -d的人,通过它在eclipse中创建的文件被删除,
你也可以在UI中使用“restore from local history”为ref: restore from local history进行同样的操作
下面定义了一个可重用的Git命令别名来删除任何本地更改,然后可以在将来的任何时候使用它来删除任何未提交的更改:
git config --global alias.remove-changes '!git stash push --include-untracked && git stash drop'
使用别名很简单:
git remove-changes
别名使用git stash push—include-untracked将所有更改推入存储(包括未提交的),然后使用git stash drop删除新创建的存储条目。
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
典型的使用是在构建脚本中,当你必须确保你的树是绝对干净的——没有任何修改或本地创建的目标文件或构建工件,并且你想让它工作得非常快,而不是每次都重新克隆整个存储库。
国家从一个承诺过渡到新的承诺
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配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 如何使用文件的行作为命令的参数?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- BAT文件:打开新的cmd窗口并在其中执行命令
- 如何做一个非贪婪匹配在grep?