Git-清理工作目录

写在前面:你需要谨慎地使用git clean这个命令,因为它被设计为从工作目录中移除未被追踪的文件。 如果你改变主意了,你也不一定能找回来那些文件的内容。相较之下,使用stash储藏或者在清理之前使用-n模拟清除是较为稳妥的方法。

下面是git clean的后缀及说明:

1
2
3
4
5
6
7
8
9
-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 clean -d

  • 移除所有未追踪的文件以及空的子目录:
    $ git clean -f -d
    (-f 意味着 _强制_ 或 “确定移除”)

  • 做一次移除演示:
    $ git clean -d -n
    (使用 -n 选项运行命令,这意味着 “做一次演习——将要移除什么”。)

    默认情况下,git clean 命令只会移除没有忽略的未跟踪文件。

如果想要移除.gitiignore 或其他忽略文件中的模式匹配的文件,则需要在clean后加 -x

  • 使用-i(interactive cleaning交互式清理)会以交互模式运行 clean 命令,这种方式下可以分别地检查每一个文件或者交互地指定删除的模式。也是一种比较稳妥的清理方式。

以上内容参考自:官方文档-7.3 Git 工具-储藏与清理

(END)