如何从当前工作树中删除未追踪的本地文件?


当前回答

要删除未追踪的文件,您应该首先使用命令查看将受清洁影响的文件。

git clean -fdn

这将向您展示将被删除的文件列表. 现在实际上删除这些文件使用这个命令:

git clean -fd

其他回答

如果需要从特定子目录中删除未追踪的文件,

git clean -f {dir_path}

并结合的方式来删除未追踪的Dir/文件和被忽略的文件。

git clean -fxd {dir_path}

此后,您将仅在 git 状态中修改文件。

用途: git clean [-d] [-f] [-i] [-n] [-q] [-e ] [-x♰ -X] [--]...

-q, --quiet           do not print names of files removed
-n, --dry-run         dry run
-f, --force           force
-i, --interactive     interactive cleaning
-d                    remove whole directories
-e, --exclude <pattern>
                      add <pattern> to ignore rules
-x                    remove ignored files, too
-X                    remove only ignored files

如果您只想删除被“吉特状态”追踪的文件

git stash save -u
git stash drop "stash@{0}"

我更喜欢“清洁”这个,因为“清洁”会删除被Git忽略的文件,所以你的下一个构建将不得不重建一切,你可能会失去你的IDE设置。

对于我刚刚发明并尝试的这种情况的一种生活哈克(这非常好):

git add .
git reset --hard HEAD

注意! 请确保在执行此事之前进行任何必要的更改(即使在未被拖动的文件中)。

git-clean - 从工作树中删除未追踪的文件 同步 git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x♰ -X] [--] <path>... 描述 通过从当前目录开始,不受版本控制的文件重新删除工作树。


# Print out the list of files and directories which will be removed (dry run)
git clean -n -d

# Delete the files from the repository
git clean -f

注意后两个命令的 X 中的案例差异。