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


当前回答

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

git add .
git stash 

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

其他回答

用途: 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 工作树中删除本地未追踪的文件,使用下面的 git 评论。

git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

例子:

git reset --hard HEAD

左:

https://git-scm.com/docs/git-reset 如何使用‘git reset --hard HEAD’ 返回以前的承诺? 恢复本地存储分支 就像远程存储分支 HEAD https://jwiegley.github.io/git-from-the-bottom-up/3-Reset/4-doing-a-hard-reset.html

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 -A& git commit -m temp && git reset --hard HEAD^

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

在运行清洁命令之前,总是使用 -n,因为它会向您展示哪些文件会被删除。

通常情况下,当没有确定的情况下,Git Clean 不会返回未跟踪的目录,以避免删除太多。 指定 -d 以便返回此类目录。 如果任何路径是确定的, -d 是无关紧要的; 所有未跟踪的文件符合确定的路径(除非在 --force 中提到的未跟踪的Git目录)将被删除。

git clean -n -d 
git clean -n -d -f

git clean -d -f

默认情况下, git clean 只会删除未追踪的文件,这些文件不会被忽视. 任何符合您的.gitignore 或其他 ignore 文件中的模式的文件将不会被删除. 如果您想要删除这些文件,您可以将 -x 添加到清洁命令。

git clean -f -d -x

git clean -x -i

If you are not 100% sure that deleting your uncommitted work is safe, you could use stashing instead
git stash --all

git stash push --keep-index

呼叫 git stash 没有任何论点,相当于 git stash push。

git stash push -m "name your stash" // before git stash save (deprecated)

git stash drop // or clean

要查看关于如何使用 stash 的完整指示,请参见此 如何在 git 中以名称命名和获取 stash?