How to delete all changes from working directory including new untracked files. I know that git checkout -f
does that, but it doesn't delete new untracked files created since last commit.
如何从工作目录中删除所有更改,包括新的未跟踪文件。我知道git checkout -f会这么做,但它不会删除自上次提交以来创建的新的未跟踪文件。
Does anybody have an idea how to do that?
有人知道怎么做吗?
12 个解决方案
#1
1357
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)
#2
211
Safest method, which I use frequently:
最安全的方法,我经常使用:
git clean -fd
#3
156
For all unstaged files use:
所有未分阶段的文件使用:
git checkout -- .
The .
at the end is important.
的。最后是很重要的。
You can replace .
with a sub-directory name to clear only a specific sub-directory of your project. The problem is addressed specifically here.
你可以替换。使用子目录名只清除项目的特定子目录。这个问题在这里得到了明确的解决。
#4
56
Have a look at the git clean
command.
查看git clean命令。
git-clean - Remove untracked files from the working tree
清理单元——从工作树中删除未跟踪的文件
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
通过递归地删除不在版本控制下的文件,从当前目录开始清理工作树。
Normally, only files unknown to git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
通常,只删除git未知的文件,但是如果指定了-x选项,也会删除被忽略的文件。例如,这对于删除所有构建产品是有用的。
#5
36
The following works:
以下工作:
git add -A .
git stash
git stash drop stash@{0}
Please note that this will discard both your unstaged and staged local changes. So you should commit anything you want to keep, before you run these commands.
请注意,这将丢弃未分阶段的本地更改和分段的本地更改。因此,在运行这些命令之前,应该提交任何想要保存的内容。
A typical use case: You moved a lot of files or directories around, and then want to get back to the original state.
一个典型的用例:您移动了许多文件或目录,然后想要回到原始状态。
Credits: https://*.com/a/52719/246724
学分:https://*.com/a/52719/246724
#6
25
You can do this in two steps:
你可以通过以下两个步骤来完成:
- Revert modified files:
git checkout -f
- 还原修改后的文件:git checkout -f。
- Remove untracked files:
git clean -fd
- 删除未跟踪的文件:git clean -fd。
#7
22
I thought it was (warning: following will wipe out everything)
我以为是(警告:接下来会把一切都抹掉)
$ git reset --hard HEAD
$ git clean -fd
The reset to undo changes. The clean to remove any untracked files and directories.
重置以撤消更改。清除删除任何未跟踪的文件和目录。
#8
5
git reset --hard origin/{branchName}
It will delete all untracked files.
它将删除所有未跟踪的文件。
#9
2
An alternative solution is to commit the changes, and then get rid of those commits. This does not have an immediate benefit at first, but it opens up the possibility to commit in chunks, and to create a git tag for backup.
另一种解决方案是提交更改,然后删除这些提交。这在一开始并没有直接的好处,但是它打开了以块形式提交的可能性,并为备份创建一个git标记。
You can do it on the current branch, like this:
你可以在当前的分支上这样做:
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^^
Or you can do it on detached HEAD. (assuming you start on BRANCHNAME branch):
或者你可以用分离的头来做。(假设你从分公司开始):
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
However, what I usually do is to commit in chunks, then name some or all commits as "DISCARD: ...". Then use interactive rebase to remove the bad commits and keep the good ones.
然而,我通常做的是用块来提交,然后命名一些或全部提交为“丢弃:…”。然后使用交互式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".
This is more verbose, but it allows to review exactly which changes you want to discard.
这更加冗长,但是它允许检查您想要删除的更改。
The git lol
and git lola
shortcuts have been very helpful with this workflow.
git lol和git lola快捷方式对这个工作流非常有用。
#10
2
git clean -i
will first show you the items to be deleted and proceed after your confirmation. I find this useful when dealing with important files that should not be deleted accidentally.
git clean -我将首先向您显示要删除的条目,并在您确认后继续。我发现这在处理重要文件时很有用,这些文件不应该被意外删除。
See git help clean
for more information, including some other useful options.
有关更多信息,请参见git帮助清理,包括一些其他有用的选项。
#11
1
For a specific folder I used:
对于我使用的特定文件夹:
git checkout -- FolderToClean/*
#12
1
If you want to discard all changes, you can use any of the valid options in an alias in .gitconfig
. For instance:
如果要放弃所有更改,可以在.gitconfig中的别名中使用任何有效选项。例如:
[alias]
discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"
Usage: git discard
用法:git丢弃
#1
1357
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)
#2
211
Safest method, which I use frequently:
最安全的方法,我经常使用:
git clean -fd
#3
156
For all unstaged files use:
所有未分阶段的文件使用:
git checkout -- .
The .
at the end is important.
的。最后是很重要的。
You can replace .
with a sub-directory name to clear only a specific sub-directory of your project. The problem is addressed specifically here.
你可以替换。使用子目录名只清除项目的特定子目录。这个问题在这里得到了明确的解决。
#4
56
Have a look at the git clean
command.
查看git clean命令。
git-clean - Remove untracked files from the working tree
清理单元——从工作树中删除未跟踪的文件
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
通过递归地删除不在版本控制下的文件,从当前目录开始清理工作树。
Normally, only files unknown to git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
通常,只删除git未知的文件,但是如果指定了-x选项,也会删除被忽略的文件。例如,这对于删除所有构建产品是有用的。
#5
36
The following works:
以下工作:
git add -A .
git stash
git stash drop stash@{0}
Please note that this will discard both your unstaged and staged local changes. So you should commit anything you want to keep, before you run these commands.
请注意,这将丢弃未分阶段的本地更改和分段的本地更改。因此,在运行这些命令之前,应该提交任何想要保存的内容。
A typical use case: You moved a lot of files or directories around, and then want to get back to the original state.
一个典型的用例:您移动了许多文件或目录,然后想要回到原始状态。
Credits: https://*.com/a/52719/246724
学分:https://*.com/a/52719/246724
#6
25
You can do this in two steps:
你可以通过以下两个步骤来完成:
- Revert modified files:
git checkout -f
- 还原修改后的文件:git checkout -f。
- Remove untracked files:
git clean -fd
- 删除未跟踪的文件:git clean -fd。
#7
22
I thought it was (warning: following will wipe out everything)
我以为是(警告:接下来会把一切都抹掉)
$ git reset --hard HEAD
$ git clean -fd
The reset to undo changes. The clean to remove any untracked files and directories.
重置以撤消更改。清除删除任何未跟踪的文件和目录。
#8
5
git reset --hard origin/{branchName}
It will delete all untracked files.
它将删除所有未跟踪的文件。
#9
2
An alternative solution is to commit the changes, and then get rid of those commits. This does not have an immediate benefit at first, but it opens up the possibility to commit in chunks, and to create a git tag for backup.
另一种解决方案是提交更改,然后删除这些提交。这在一开始并没有直接的好处,但是它打开了以块形式提交的可能性,并为备份创建一个git标记。
You can do it on the current branch, like this:
你可以在当前的分支上这样做:
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^^
Or you can do it on detached HEAD. (assuming you start on BRANCHNAME branch):
或者你可以用分离的头来做。(假设你从分公司开始):
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
However, what I usually do is to commit in chunks, then name some or all commits as "DISCARD: ...". Then use interactive rebase to remove the bad commits and keep the good ones.
然而,我通常做的是用块来提交,然后命名一些或全部提交为“丢弃:…”。然后使用交互式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".
This is more verbose, but it allows to review exactly which changes you want to discard.
这更加冗长,但是它允许检查您想要删除的更改。
The git lol
and git lola
shortcuts have been very helpful with this workflow.
git lol和git lola快捷方式对这个工作流非常有用。
#10
2
git clean -i
will first show you the items to be deleted and proceed after your confirmation. I find this useful when dealing with important files that should not be deleted accidentally.
git clean -我将首先向您显示要删除的条目,并在您确认后继续。我发现这在处理重要文件时很有用,这些文件不应该被意外删除。
See git help clean
for more information, including some other useful options.
有关更多信息,请参见git帮助清理,包括一些其他有用的选项。
#11
1
For a specific folder I used:
对于我使用的特定文件夹:
git checkout -- FolderToClean/*
#12
1
If you want to discard all changes, you can use any of the valid options in an alias in .gitconfig
. For instance:
如果要放弃所有更改,可以在.gitconfig中的别名中使用任何有效选项。例如:
[alias]
discard = "!f() { git add . && git stash && git stash drop stash@{0}; }; f"
Usage: git discard
用法:git丢弃