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


当前回答

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

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

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

其他回答

这是我一直使用的:

git clean -fdx

对于一个非常大的项目,你可能想运行它几次。

如果没有其他工作,可以使用下列组合来简单地删除“吉特状态”命令列出的所有变化:

git add -A& git commit -m temp && git reset --hard HEAD^

这将首先完成您的所有变化,然后创建一个暂时的承诺,然后放弃它。

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 列表中删除所有未追踪的文件。

要知道在实际删除之前会删除什么:

清洁 -d -n

它会产生这样的东西:

将移除 sample.txt

要删除前命令输出中列出的所有内容:

清洁 -d -f

它会产生这样的东西:

删除 sample.txt

这可以用一个阴影脚本进行,我使用这个脚本列出什么将被删除,然后让我确认操作。

这是有用的,因为有时我有插件或其他文件我会喜欢检查之前删除一切。

#!/bin/bash
readarray -t -d '' FILES < <(git ls-files -z --other --directory)
if [ "$FILES" = "" ]; then
    echo  "Nothing to clean!"
    exit 0
fi
echo -e "Dirty files:\n"
printf '  %s\n' "${FILES[@]}"
DO_REMOVE=0
while true; do
    echo ""
    read -p "Remove ${#FILES[@]} files? [y/n]: " choice
    case "$choice" in
        y|Y )
            DO_REMOVE=1
            break ;;
        n|N )
            echo "Exiting!"
            break ;;
        * ) echo "Invalid input, expected [Y/y/N/n]"
            continue ;;
    esac
done

if [ "$DO_REMOVE" -eq 1 ];then
    echo "Removing!"
    for f in "${FILES[@]}"; do
        rm -rfv -- "$f"
    done
fi