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


当前回答

如果未追踪的目录是自己的 git 存储库(例如子模块),您需要使用 -f 两次:

清洁 -d -f -f

其他回答

git add --all, git stash and git stash drop, try these three commands in this order in order to remove all untracked files. 通过添加所有这些未追踪的文件到 git 并 stashing 它们将移动所有这些未追踪的文件到 stash 列表并下载顶部一个,即, stash@{0} 将从 stash 列表中删除所有未追踪的文件。

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 中的案例差异。


我认为最安全、最容易的方式就是这样!

git add .
git stash 

更多信息 https://www.atlassian.com/git/tutorials/saving-changes/git-stash#stashing-your-work

oh-my-zsh 与 zsh 提供这些伟大的联盟通过 git 插件. 它们也可以在 bash 中使用。

gclean='git clean -fd' gpristine='git reset --hard && git clean -dfx'

gclean 除去未追踪的目录除了未追踪的文件. gpristine 硬重定义本地变更,删除未追踪的目录,未追踪的文件,不要使用从.gitignore (每个目录) 和 $GIT_DIR/info/exclude 阅读的默认忽略规则,但仍然使用与 -e 选项提供的忽略规则。

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

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

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