本文介绍了如何清除我在 Git 中的本地工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何清除我在 Git 中的工作目录?

How can I clear my working directory in Git?

推荐答案

要将特定文件重置为上次提交状态(放弃特定文件中未提交的更改):

To reset a specific file to the last-committed state (to discard uncommitted changes in a specific file):

git checkout thefiletoreset.txt

这在 git status 输出中提到:

(use "git checkout -- <file>..." to discard changes in working directory)

要将整个存储库重置为上次提交的状态:

To reset the entire repository to the last committed state:

git reset --hard

要删除未跟踪的文件,我通常只删除工作副本中的所有文件(但不是.git/ 文件夹!),然后执行 git reset--hard 只留下提交的文件.

To remove untracked files, I usually just delete all files in the working copy (but not the .git/ folder!), then do git reset --hard which leaves it with only committed files.

更好的方法是使用git clean(警告:使用下面的-x标志会导致Git删除被忽略的文件):

A better way is to use git clean (warning: using the -x flag as below will cause Git to delete ignored files):

git clean -d -x -f

将删除未跟踪的文件,包括目录 (-d) 和被 git 忽略的文件 (-x).将 -f 参数替换为 -n 以执行试运行或 -i 为交互模式,它会告诉您将是什么已移除.

will remove untracked files, including directories (-d) and files ignored by git (-x). Replace the -f argument with -n to perform a dry-run or -i for interactive mode, and it will tell you what will be removed.

相关链接:

  • git-reset man page
  • git-clean man page
  • git ready "cleaning up untracked files" (as Marko posted)
  • Stack Overflow question "How to remove local (untracked) files from the current Git working tree")

这篇关于如何清除我在 Git 中的本地工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:27