如何从当前Git工作树中删除本地(未跟踪)文件

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


#1楼

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

git clean -d -f -f


#2楼

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

git clean -f {dir_path}

以及删除未跟踪目录/文件和忽略文件的组合方式。

git clean -fxd {dir_path}

之后,您将只能以git status修改文件。


#3楼

这就是我一直使用的:

git clean -fdx

对于一个非常大的项目,您可能需要运行几次。


#4楼

令我惊讶的是,之前没有人提到过:

git clean -i

这表示交互式 ,您将快速了解要删除的内容,从而可以包含/排除受影响的文件。 总体而言,仍比真正清洁之前运行强制性--dry-run更快。

如果您还想处理空文件夹,则必须输入-d 。 最后,它提供了一个很好的别名:

git iclean

话虽如此,交互命令的额外手握可能会使有经验的用户感到厌烦。 这些天我只用已经提到过的git clean -fd


#5楼

我喜欢git stash push -u因为您可以使用git stash pop撤消它们。

编辑:我也找到了一种方法来显示隐藏的未跟踪文件(例如git show stash@{0}^3https://stackoverflow.com/a/12681856/338986

EDIT2:不推荐使用git stash save而推荐使用push 。 谢谢@ script-wolf。


#6楼

git clean -fd删除目录

git clean -fX删除忽略的文件

git clean -fx删除被忽略和未被忽略的文件

可以结合使用以上所有选项

git clean -fdXx

检查git手册以获得更多帮助


#7楼

更好的方法是使用:git clean

git clean -d -x -f

这将删除未跟踪的文件,包括目录(-d)git (-x)忽略的文件。

另外,将-f参数替换为-n以执行dry-run或者将-i替换为交互式模式,它将告诉您要删除的内容。


#8楼

git clean -f -d -x $(git rev-parse --show-cdup)将clean应用于根目录,无论您在存储库目录树中的何处调用它。 我一直在使用它,因为它不会强迫您离开您现在使用的文件夹,并且可以从您所在的位置直接进行清理和提交。

确保标志-f-d-x满足您的需求:

-d
       Remove untracked directories in addition to untracked files. If an
       untracked directory is managed by a different Git repository, it is
       not removed by default. Use -f option twice if you really want to
       remove such a directory.

-f, --force
       If the Git configuration variable clean.requireForce is not set to
       false, git clean will refuse to delete files or directories unless
       given -f, -n or -i. Git will refuse to delete directories with .git
       sub directory or file unless a second -f is given. This affects
       also git submodules where the storage area of the removed submodule
       under .git/modules/ is not removed until -f is given twice.

-x
       Don't use the standard ignore rules read from .gitignore (per
       directory) and $GIT_DIR/info/exclude, but do s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值