如何从工作目录删除所有更改,包括新的未跟踪文件。我知道git checkout -f这样做,但它不会删除自上次提交以来创建的新的未跟踪文件。

有人知道怎么做吗?


当前回答

git reset --hard # removes staged and working directory changes

## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)

要查看将事先删除的内容,而不实际删除它,请使用-n标志(这基本上是一个测试运行)。当你准备实际删除时,然后删除-n标志:

Git clean -nfd

其他回答

git reset --hard origin/{branchName}

它将删除所有未跟踪的文件。

如果希望放弃所有更改,可以在.gitconfig中的别名中使用任何有效选项。例如:

[alias]
    discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"

用法:git discard

git reset --hard # removes staged and working directory changes

## !! be very careful with these !!
## you may end up deleting what you don't want to
## read comments and manual.
git clean -f -d # remove untracked
git clean -f -x -d # CAUTION: as above but removes ignored files like config.
git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without :/, the operation affects only the current directory)

要查看将事先删除的内容,而不实际删除它,请使用-n标志(这基本上是一个测试运行)。当你准备实际删除时,然后删除-n标志:

Git clean -nfd

以下工作:

git add -A .
git stash
git stash drop stash@{0}

请注意,这将丢弃您的非分段和分段本地更改。因此,在运行这些命令之前,您应该提交想要保留的任何内容。

一个典型的用例:您移动了许多文件或目录,然后希望恢复到原始状态。

学分:https://stackoverflow.com/a/52719/246724

对于所有被跟踪的非暂存文件使用:

git checkout -- .

的。结尾很重要。

你可以替换。子目录名,可仅清除项目的特定子目录。这里专门讨论了这个问题。