是否有一个Git命令可以恢复工作树和索引中所有未提交的更改,并删除新创建的文件和文件夹?
您可以执行以下两条命令:
# Revert changes to modified files.
git reset --hard
# Remove all untracked files and directories.
# '-f' is force, '-d' is remove directories.
git clean -fd
git clean -fd
没有帮助,新的文件仍然存在。我完全删除了所有工作树,然后
git reset --hard
在“https://stackoverflow.com/questions/673407/how-do-i-clear-my-local-working-directory-in-git/673420#673420”中添加-x选项的建议:
git clean -fdx
注意-x标志将删除所有被Git忽略的文件,所以要小心(参见我引用的答案中的讨论)。
使用“git checkout——…”来丢弃工作目录中的更改
git checkout -- app/views/posts/index.html.erb
or
git checkout -- *
删除在git状态下对非暂存文件所做的所有更改
modified: app/controllers/posts.rb
modified: app/views/posts/index.html.erb
如果您只想还原当前工作目录中的更改,请使用
git checkout -- .
在此之前,你可以列出将被恢复的文件,而不实际做任何操作,只是检查将会发生什么,用:
git checkout --
Use:
git reset HEAD filepath
例如:
git reset HEAD om211/src/META-INF/persistence.xml
请注意,可能仍然有文件看起来不会消失——它们可能是未编辑的,但Git可能因为CRLF / LF更改而将它们标记为正在编辑。看看你最近是否在.gitattributes中做了一些更改。
在我的例子中,我将CRLF设置添加到.gitattributes文件中,因此所有文件都保留在“修改文件”列表中。更改.gitattributes设置会使它们消失。
你可以使用下面的Git命令,它可以恢复你仓库中所有未提交的更改:
git checkout .
例子:
ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: application/controllers/Drivers.php
modified: application/views/drivers/add.php
modified: application/views/drivers/load_driver_info.php
modified: uploads/drivers/drivers.xlsx
no changes added to commit (use "git add" and/or "git commit -a")
ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git checkout .
ABC@ABC-PC MINGW64 /c/xampp/htdocs/pod_admin (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
Git 2.23引入了Git restore命令来恢复工作树文件。
恢复当前目录下的所有文件。
git restore .
如果您想恢复所有C源文件以匹配索引中的版本,您可以这样做
git restore '*.c'
从Git帮助:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
推荐文章
- 如何将git配置存储为存储库的一部分?
- 如何修改GitHub拉请求?
- 如何在Github和本地删除最后n次提交?
- 我如何调试git/git-shell相关的问题?
- 错误:无法使用rebase进行拉取:您有未分阶段的更改
- Git隐藏未缓存:如何把所有未分期的变化?
- 真实的恶魔
- 如何从另一个分支获得更改
- Git:权限被拒绝(publickey)致命-无法从远程存储库读取。克隆Git存储库时
- git reflog和log有什么区别?
- git推挂在Total line之后
- 重命名git子模块
- 结合Git存储库的前两次提交?
- Xcode 6 gitignore文件应该包括什么?
- 我如何从现有回购的分支创建一个新的GitHub回购?