如何从工作目录删除所有更改,包括新的未跟踪文件。我知道git checkout -f这样做,但它不会删除自上次提交以来创建的新的未跟踪文件。
有人知道怎么做吗?
如何从工作目录删除所有更改,包括新的未跟踪文件。我知道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 # 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 checkout -- .
的。结尾很重要。
你可以替换。子目录名,可仅清除项目的特定子目录。这里专门讨论了这个问题。
这可能是一个新手的答案,但是: 我在窗口中使用TortoiseGit,它有一个很好的功能叫做REVERT。 所以你要做的是恢复你的本地非分期非推送的更改:
调出所需文件夹的上下文菜单并选择恢复, 它显示了一个恢复弹出窗口,您可以选择已更改的文件恢复/恢复。 如果你还想删除添加的文件(还没有在git中),点击commit(从相同的上下文中) 菜单),它会弹出Commit弹出窗口,然后显示添加的文件 右键单击它们并选择删除。但不要按“提交” BTN在这个弹出窗口,因为你不想提交,但只看到添加 文件,然后从这里删除。
另一种解决方案是提交更改,然后删除这些提交。这在一开始并没有立即的好处,但它提供了以块提交的可能性,并为备份创建了一个git标记。
你可以在当前分支上这样做:
git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01 # optional
git revert HEAD
git reset HEAD^^
或者你可以在分离的HEAD上做。(假设你从BRANCHNAME分支开始):
git checkout --detach HEAD
git add (-A) .
git commit -m"DISCARD: Temporary local changes"
git tag archive/local-changes-2015-08-01 # optional
git checkout BRANCHNAME
然而,我通常做的是在块中提交,然后将一些或所有提交命名为“DISCARD:…”。然后使用交互式rebase删除坏的提交并保留好的提交。
git add -p # Add changes in chunks.
git commit -m"DISCARD: Some temporary changes for debugging"
git add -p # Add more stuff.
git commit -m"Docblock improvements"
git tag archive/local-changes-2015-08-01
git rebase -i (commit id) # rebase on the commit id before the changes.
# Remove the commits that say "DISCARD".
这是更详细的,但它允许准确地检查您想要丢弃的更改。
git lol和git lola快捷键对这个工作流非常有帮助。
Git clean -i将首先向您显示要删除的项目,并在确认后继续执行。我发现这在处理不应该被意外删除的重要文件时很有用。
有关更多信息,请参阅git help clean,包括一些其他有用的选项。