How do I discard changes in my working copy that are not in the index?
如何丢弃不在索引中的工作副本中的更改?
32 个解决方案
#1
2128
Another quicker way is:
另一个简单的方法就是:
git stash save --keep-index
Include --include-untracked
if you'd want to be thorough about it.
如果你想彻底了解的话,可以把它包括在内。
After that, you can drop that stash with a git stash drop
command if you like.
在此之后,如果您愿意,您可以使用git隐藏命令删除该隐藏文件。
#2
4100
For all unstaged files use:
对于所有未分阶段的文件使用:
git checkout -- .
For a specific file use:
对于特定的文件使用:
git checkout path/to/file/to/revert
Make sure to include the period at the end.
最后一定要写上句号。
#3
1594
It seems like the complete solution is:
似乎完整的解决方案是:
git clean -df
git checkout -- .
git clean
removes all untracked files (warning: while it won't delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders) and git checkout
clears all unstaged changes.
git clean删除所有未跟踪的文件(警告:虽然它不会删除直接在.gitignore中提到的被忽略的文件,但是它可以删除文件夹中被忽略的文件),git checkout清除所有未分阶段的更改。
#4
268
This checks out the current index for the current directory, throwing away all changes in files from the current directory downwards.
这将检查当前目录的当前索引,并将文件中的所有更改从当前目录向下抛出。
git checkout .
or this which checks out all files from the index, overwriting working tree files.
或者检查索引中的所有文件,覆盖工作树文件。
git checkout-index -a -f
#5
208
git clean -df
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
通过递归地删除不在版本控制下的文件,从当前目录开始清理工作树。
-d
: Remove untracked directories in addition to untracked files
-d:除去未跟踪文件之外的未跟踪目录
-f
: Force (might be not necessary depending on clean.requireForce
setting)
-f:用力(可能不需要,取决于干净。requireForce设置)
Run git help clean
to see the manual
运行git帮助查看手册
#6
78
My favorite is
我最喜欢的是
git checkout -p
That lets you selectively revert chunks.
这允许您选择性地恢复块。
See also:
参见:
git add -p
#7
62
Since no answer suggests the exact option combination that I use, here it is:
因为没有答案表明我使用的确切的选项组合,这里是:
git clean -dfx
git checkout .
This is the online help text for the used git clean
options:
这是在线帮助文本为使用git清洁选项:
-d
- d
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f
option twice if you really want to remove such a directory.
除了未跟踪的文件之外,删除未跟踪的目录。如果未跟踪的目录由不同的Git存储库管理,则默认情况下不会删除它。如果您真的想删除这样的目录,请使用-f选项两次。
-f
- f
If the Git configuration variable clean.requireForce
is not set to false
, Git clean will refuse to delete files or directories unless given -f
, -n
, or -i
. Git will refuse to delete directories within the .git
subdirectory or file, unless a second -f
is given.
如果Git配置变量被清除。requireForce没有设置为false, Git clean将拒绝删除文件或目录,除非给出-f、-n或-i。Git将拒绝删除. Git子目录或文件中的目录,除非给出第二个-f。
-x
- x
Don’t use the ignore rules from .gitignore
(per directory) and $GIT_DIR/info/exclude
, but do still use the ignore rules given with -e
options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset
) to create a pristine working directory to test a clean build.
不要使用来自.gitignore(每个目录)和$GIT_DIR/info/ exclusion的忽略规则,但是仍然要使用带有-e选项的忽略规则。这允许删除所有未跟踪的文件,包括构建产品。可以使用它(可能与git reset一起使用)创建一个原始的工作目录来测试干净的构建。
Also, git checkout .
needs to be done in the root of the repo.
此外,git checkout。需要在回购的根上完成。
#8
49
I really found this article helpful for explaining when to use what command: http://www.szakmeister.net/blog/2011/oct/12/reverting-changes-git/
我发现这篇文章对解释什么时候使用什么命令很有帮助:http://www.szakmeister.net/blog/2011/oct/12/reverting- changgit/
There are a couple different cases:
有几个不同的案例:
-
If you haven't staged the file, then you use
git checkout
. Checkout "updates files in the working tree to match the version in the index". If the files have not been staged (aka added to the index)... this command will essentially revert the files to what your last commit was.如果没有对文件进行分段,那么使用git签出。签出“更新工作树中的文件以匹配索引中的版本”。如果文件没有分段(也就是添加到索引中)……该命令将文件还原为您上次提交的内容。
git checkout -- foo.txt
git checkout——foo.txt
-
If you have staged the file, then use git reset. Reset changes the index to match a commit.
如果已经对文件进行了分段,那么使用git reset。重置修改索引以匹配提交。
git reset -- foo.txt
git重置,foo.txt
I suspect that using git stash
is a popular choice since it's a little less dangerous. You can always go back to it if you accidently blow too much away when using git reset. Reset is recursive by default.
我怀疑使用git stab是一种流行的选择,因为它没有那么危险。如果您在使用git复位时不小心吹走了太多,您可以回到它。缺省情况下,重置是递归的。
Take a look at the article above for further advice.
看看上面的文章,寻求更多的建议。
#9
44
The easiest way to do this is by using this command:
最简单的方法是使用以下命令:
This command is used to discard changes in working directory -
此命令用于丢弃工作目录-中的更改
git checkout -- .
https://git-scm.com/docs/git-checkout
https://git-scm.com/docs/git-checkout
In git command, stashing of untracked files is achieved by using:
在git命令中,未跟踪文件的存储通过以下方式实现:
git stash -u
http://git-scm.com/docs/git-stash
http://git-scm.com/docs/git-stash
#10
39
If you aren't interested in keeping the unstaged changes (especially if the staged changes are new files), I found this handy:
如果您对保持未分阶段的更改不感兴趣(特别是在阶段更改为新文件时),我发现这很方便:
git diff | git apply --reverse
#11
38
As you type git status, (use "git checkout -- ..." to discard changes in working directory) is shown.
当您输入git状态时,(使用“git checkout -…”在工作目录中丢弃更改)将显示出来。
e.g. git checkout -- .
例如git签出——
#12
36
git checkout -f
git checkout - f
man git-checkout
:
男人git-checkout:
-f, --force
- f,力
When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.
当切换分支时,即使索引或工作树与HEAD不同,也要继续。这用于丢弃本地更改。
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.
在从索引检出路径时,不要在未合并的项上失败;相反,未合并的条目将被忽略。
#13
33
You can use git stash - if something goes wrong, you can still revert from the stash. Similar to some other answer here, but this one also removes all unstaged files and also all unstaged deletes:
您可以使用git存储—如果出现问题,您仍然可以从该存储中恢复。类似于这里的其他答案,但是这个也删除了所有的非分阶段文件和所有的非分阶段删除:
git add .
git stash
if you check that everything is OK, throw the stash away:
如果你检查一切都好,扔掉这些东西:
git stash drop
The answer from Bilal Maqsood with git clean
also worked for me, but with the stash I have more control - if I do sth accidentally, I can still get my changes back
从Bilal Maqsood和git clean的答案也为我工作,但我有更多的控制权——如果我不小心做了某件事,我仍然可以找回我的改变。
UPDATE
更新
I think there is 1 more change (don't know why this worked for me before):
我觉得还有一个变化(不知道为什么这个对我有用):
git add . -A
instead of git add .
git添加。-而不是git add。
without the -A
the removed files will not be staged
没有-A,删除的文件将不会被暂存
#14
30
Instead of discarding changes, I reset my remote to the origin. Note - this method is to completely restore your folder to that of the repo.
我没有丢弃更改,而是将遥控器重置到原点。注意-这个方法是完全恢复你的文件夹到那个回购。
So I do this to make sure they don't sit there when I git reset (later - excludes gitignores on the Origin/branchname)
所以我这样做是为了确保当我重新设置时它们不在那里(稍后——不包括在原点/分支名上的gitignores)
NOTE: If you want to keep files not yet tracked, but not in GITIGNORE you may wish to skip this step, as it will Wipe these untracked files not found on your remote repository (thanks @XtrmJosh).
注意:如果您想保持文件未被跟踪,但不在GITIGNORE中,您可能希望跳过这一步,因为它将擦除远程存储库中未被跟踪的文件(谢谢@XtrmJosh)。
git add --all
Then I
然后我
git fetch --all
Then I reset to origin
然后重置到原点
git reset --hard origin/branchname
That will put it back to square one. Just like RE-Cloning the branch, WHILE keeping all my gitignored files locally and in place.
它会回到1的平方。就像重新克隆分支一样,同时在本地和适当的地方保留我所有的gitignored文件。
Updated per user comment below: Variation to reset the to whatever current branch the user is on.
更新每个用户评论下面:变化重置到任何当前的分支用户是在。
git reset --hard @{u}
#15
22
Tried all the solutions above but still couldn't get rid of new, unstaged files.
尝试了以上所有的解决方案,但仍然无法摆脱新的、未分阶段的文件。
Use git clean -f
to remove those new files - with caution though! Note the force option.
使用git clean -f删除那些新文件——但是要小心!注意力量的选择。
#16
19
Just use:
只使用:
git stash -u
Done. Easy.
完成了。一件容易的事。
If you really care about your stash stack then you can follow with git stash drop
. But at that point you're better off using (from Mariusz Nowak):
如果您真的关心您的隐藏堆栈,那么您可以使用git隐藏删除。但这时你最好使用(来自Mariusz Nowak):
git checkout -- .
git clean -df
Nonetheless, I like git stash -u
the best because it "discards" all tracked and untracked changes in just one command. Yet git checkout -- .
only discards tracked changes, and git clean -df
only discards untracked changes... and typing both commands is far too much work :)
尽管如此,我还是喜欢git存储库,因为它“丢弃”了所有跟踪和未跟踪的更改。但是git checkout——。只丢弃跟踪的更改,而git clean -df只丢弃未跟踪的更改……而且输入这两个命令实在是太麻烦了。
#17
17
simply say
简单地说
git stash
It will remove all your local changes. You also can use later by saying
它将删除所有本地更改。后面你也可以这样说
git stash apply
or git stash pop
或git藏流行
#18
15
This works even in directories that are; outside of normal git permissions.
这甚至可以在目录中找到;在正常的git权限之外。
sudo chmod -R 664 ./* && git checkout -- . && git clean -dfx
Happened to me recently
最近发生在我身上
#19
13
cd path_to_project_folder # take you to your project folder/working directory
git checkout . # removes all unstaged changes in working directory
#20
9
Another way to get rid of new files that is more specific than git clean -df (it will allow you to get rid of some files not necessarily all), is to add the new files to the index first, then stash, then drop the stash.
另一种处理新文件的方法是先将新文件添加到索引中,然后保存,然后删除。
This technique is useful when, for some reason, you can't easily delete all of the untracked files by some ordinary mechanism (like rm).
当出于某种原因,您不能通过一些普通的机制(比如rm)删除所有未跟踪的文件时,这种技术非常有用。
#21
9
In my opinion,
在我看来,
git clean -df
should do the trick. As per Git documentation on git clean
应该足够了。根据Git清洁文档
git-clean - Remove untracked files from the working tree
清理单元——从工作树中删除未跟踪的文件
Description
描述
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选项,也会删除被忽略的文件。例如,这对于删除所有构建产品是有用的。
If any optional ... arguments are given, only those paths are affected.
如果任何可选…给出参数,只影响这些路径。
Options
选项
-d Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.
-d除去未跟踪文件之外的未跟踪目录。如果未跟踪的目录由不同的Git存储库管理,则默认情况下不会删除它。如果您真的想删除这样的目录,请使用-f选项两次。
-f --force If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.
-f——如果Git配置变量被清除,则强制执行。没有将requireForce设置为false, git clean将拒绝运行,除非给出-f、-n或-i。
#22
8
What follows is really only a solution if you are working with a fork of a repository where you regularly synchronize (e.g. pull request) with another repo. Short answer: delete fork and refork, but read the warnings on github.
只有当您使用存储库的一个分支时(例如,提取请求),才会使用另一个repo。简短的回答:删除fork和refork,但是阅读github上的警告。
I had a similar problem, perhaps not identical, and I'm sad to say my solution is not ideal, but it is ultimately effective.
我有一个相似的问题,也许不完全相同,我很遗憾地说我的解决方案并不理想,但它最终是有效的。
I would often have git status messages like this (involving at least 2/4 files):
我经常会有这样的git状态消息(至少包含2/4个文件):
$ git status
# Not currently on any branch.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: doc/PROJECT/MEDIUM/ATS-constraint/constraint_s2var.dats
# modified: doc/PROJECT/MEDIUM/ATS-constraint/parsing/parsing_s2var.dats
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: doc/PROJECT/MEDIUM/ATS-constraint/constraint_s2Var.dats
# modified: doc/PROJECT/MEDIUM/ATS-constraint/parsing/parsing_s2Var.dats
A keen eye will note that these files have dopplegangers that are a single letter in case off. Somehow, and I have no idea what led me down this path to start with (as I was not working with these files myself from the upstream repo), I had switched these files. Try the many solutions listed on this page (and other pages) did not seem to help.
一个敏锐的眼睛会注意到,这些文件有面貌极相似的人一个字母了。不知为什么,我不知道让我走上这条路开始(我没有处理这些文件从上游回购),我不得不将这些文件。尝试在这个页面(和其他页面)列出的许多解决方案似乎没有帮助。
I was able to fix the problem by deleting my forked repository and all local repositories, and reforking. This alone was not enough; upstream had to rename the files in question to new filenames. As long as you don't have any uncommited work, no wikis, and no issues that diverge from the upstream repository, you should be just fine. Upstream may not be very happy with you, to say the least. As for my problem, it is undoubtedly a user error as I'm not that proficient with git, but the fact that it is far from easy to fix points to an issue with git as well.
通过删除我的分叉存储库和所有本地存储库并进行重新设计,我能够解决这个问题。光有这一点是不够的;上游必须将有问题的文件重命名为新的文件名。只要您没有任何未完成的工作、没有wiki,并且没有与上游存储库分离的问题,您就应该没事。退一步说,上游的人可能对你不太满意。至于我的问题,它无疑是一个用户错误,因为我并不是很精通git,但是它与git之间的问题也很不容易解决。
#23
8
No matter what state your repo is in you can always reset to any previous commit:
无论你的回购是在什么状态,你都可以重置到之前的任何承诺:
git reset --hard <commit hash>
This will discard all changes which were made after that commit.
这将丢弃在提交之后所做的所有更改。
#24
8
If you merely wish to remove changes to existing files, use checkout
(documented here).
如果您只是希望删除对现有文件的更改,请使用checkout(这里有文档说明)。
git checkout -- .
- No branch is specified, so it checks out the current branch.
- 没有指定分支,因此它检查当前分支。
- The double-hyphen (
--
) tells Git that what follows should be taken as its second argument (path), that you skipped specification of a branch. - 双连字符(-)告诉Git,以下内容应该作为第二个参数(路径),您跳过了分支的规范。
- The period (
.
) indicates all paths. - 句点(.)表示所有路径。
If you want to remove files added since your last commit, use clean
(documented here):
如果您想删除自上次提交以来添加的文件,请使用clean(这里有文档说明):
git clean -i
- The
-i
option initiates an interactiveclean
, to prevent mistaken deletions. - -i选项启动交互式清理,以防止错误删除。
- A handful of other options are available for a quicker execution; see the documentation.
- 可以使用其他一些选项来更快地执行;见文档。
If you wish to move changes to a holding space for later access, use stash
(documented here):
如果您希望将更改移动到存储空间以供以后访问,请使用隐藏(此处有文档说明):
git stash
- All changes will be moved to Git's Stash, for possible later access.
- 所有更改都将转移到Git的存储区,以便以后访问。
- A handful of options are available for more nuanced stashing; see the documentation.
- 一些选项可以用于更细微的存储;见文档。
#25
6
When you want to transfer a stash to someone else:
当你想转移一个藏匿点给别人:
# add files
git add .
# diff all the changes to a file
git diff --staged > ~/mijn-fix.diff
# remove local changes
git reset && git checkout .
# (later you can re-apply the diff:)
git apply ~/mijn-fix.diff
[edit] as commented, it ís possible to name stashes. Well, use this if you want to share your stash ;)
[编辑]正如评论所言,可以命名堆栈。如果你想分享你的收藏,就用这个吧
#26
5
If all the staged files were actually committed, then the branch can simply be reset e.g. from your GUI with about three mouse clicks: Branch, Reset, Yes!
如果所有的暂存文件都已提交,那么就可以简单地对分支进行重置,例如,从您的GUI中进行大约三次鼠标点击:branch, reset, Yes!
So what I often do in practice to revert unwanted local changes is to commit all the good stuff, and then reset the branch.
因此,在实践中,我通常要做的是恢复不需要的本地更改,即提交所有好的内容,然后重新设置分支。
If the good stuff is committed in a single commit, then you can use "amend last commit" to bring it back to being staged or unstaged if you'd ultimately like to commit it a little differently.
如果在一次提交中提交了好的内容,那么如果您最终希望提交的内容稍微有所不同,那么您可以使用“修订最后提交”将其恢复为分阶段提交或非分阶段提交。
This might not be the technical solution you are looking for to your problem, but I find it a very practical solution. It allows you to discard unstaged changes selectively, resetting the changes you don't like and keeping the ones you do.
这可能不是您正在寻找的技术解决方案,但我发现它是一个非常实用的解决方案。它允许您有选择地丢弃未分阶段的更改,重新设置您不喜欢的更改,并保留您所做的更改。
So in summary, I simply do commit, branch reset, and amend last commit.
总之,我只需要提交、分支重置和修改最后提交。
#27
5
None of the solutions work if you just changed the permissions of a file (this is on DOS/Windoze)
如果您只是更改文件的权限(这是在DOS/Windoze上),那么这些解决方案都不起作用。
Mon 23/11/2015-15:16:34.80 C:\...\work\checkout\slf4j+> git status On branch SLF4J_1.5.3 Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: .gitignore modified: LICENSE.txt modified: TODO.txt modified: codeStyle.xml modified: pom.xml modified: version.pl no changes added to commit (use "git add" and/or "git commit -a") Mon 23/11/2015-15:16:37.87 C:\...\work\checkout\slf4j+> git diff diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/TODO.txt b/TODO.txt old mode 100644 new mode 100755 diff --git a/codeStyle.xml b/codeStyle.xml old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 diff --git a/version.pl b/version.pl old mode 100644 new mode 100755 Mon 23/11/2015-15:16:45.22 C:\...\work\checkout\slf4j+> git reset --hard HEAD HEAD is now at 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore Mon 23/11/2015-15:16:47.42 C:\...\work\checkout\slf4j+> git clean -f Mon 23/11/2015-15:16:53.49 C:\...\work\checkout\slf4j+> git stash save -u Saved working directory and index state WIP on SLF4J_1.5.3: 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore HEAD is now at 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore Mon 23/11/2015-15:17:00.40 C:\...\work\checkout\slf4j+> git stash drop Dropped refs/stash@{0} (cb4966e9b1e9c9d8daa79ab94edc0c1442a294dd) Mon 23/11/2015-15:17:06.75 C:\...\work\checkout\slf4j+> git stash drop Dropped refs/stash@{0} (e6c49c470f433ce344e305c5b778e810625d0529) Mon 23/11/2015-15:17:08.90 C:\...\work\checkout\slf4j+> git stash drop No stash found. Mon 23/11/2015-15:17:15.21 C:\...\work\checkout\slf4j+> git checkout -- . Mon 23/11/2015-15:22:00.68 C:\...\work\checkout\slf4j+> git checkout -f -- . Mon 23/11/2015-15:22:04.53 C:\...\work\checkout\slf4j+> git status On branch SLF4J_1.5.3 Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: .gitignore modified: LICENSE.txt modified: TODO.txt modified: codeStyle.xml modified: pom.xml modified: version.pl no changes added to commit (use "git add" and/or "git commit -a") Mon 23/11/2015-15:22:13.06 C:\...\work\checkout\slf4j+> git diff diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/TODO.txt b/TODO.txt old mode 100644 new mode 100755 diff --git a/codeStyle.xml b/codeStyle.xml old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 diff --git a/version.pl b/version.pl old mode 100644 new mode 100755
The only way to fix this is to manually reset the permissions on the changed files:
唯一的解决方法是手动重置修改文件的权限:
Mon 23/11/2015-15:25:43.79 C:\...\work\checkout\slf4j+> git status -s | egrep "^ M" | cut -c4- | for /f "usebackq tokens=* delims=" %A in (`more`) do chmod 644 %~A Mon 23/11/2015-15:25:55.37 C:\...\work\checkout\slf4j+> git status On branch SLF4J_1.5.3 nothing to commit, working directory clean Mon 23/11/2015-15:25:59.28 C:\...\work\checkout\slf4j+> Mon 23/11/2015-15:26:31.12 C:\...\work\checkout\slf4j+> git diff
#28
5
You could create your own alias which describes how to do it in a descriptive way.
您可以创建您自己的别名,该别名描述如何以一种描述性的方式进行此操作。
I use the next alias to discard changes.
我使用下一个别名来丢弃更改。
Discard changes in a (list of) file(s) in working tree
discard = checkout --
Then you can use it as next to discard all changes:
然后你可以使用它作为下一个放弃所有的改变:
discard .
Or just a file:
或者只是一个文件:
discard filename
Otherwise, if you want to discard all changes and also the untracked files, I use a mix of checkout and clean:
否则,如果您想丢弃所有更改和未跟踪的文件,我将使用校验和清理的混合:
Clean and discard changes and untracked files in working tree
cleanout = !git clean -df && git checkout -- .
So the use is simple as next:
因此,下面的用法很简单:
cleanout
Now is available in the next Github repo which contains a lot of aliases:
现在可以在下一个Github repo中找到,它包含了很多别名:
- https://github.com/GitAlias/gitalias
- https://github.com/GitAlias/gitalias
#29
5
If you are in case of submodule and no other solutions work try:
如果您是子模块,没有其他解决方案,请尝试:
-
To check what is the problem (maybe a "dirty" case) use:
检查问题是什么(可能是“脏”的情况)使用:
git diff
git diff
-
To remove stash
删除隐藏
git submodule update
git子模块更新
#30
4
I had a weird situation where a file is always unstaged, this helps me to resolve.
我有一个奇怪的情况,文件总是未分阶段,这有助于我解决问题。
git rm .gitattributes
git add -A
git reset --hardgit rm .gitattributes git添加-一个git复位——很难
#1
2128
Another quicker way is:
另一个简单的方法就是:
git stash save --keep-index
Include --include-untracked
if you'd want to be thorough about it.
如果你想彻底了解的话,可以把它包括在内。
After that, you can drop that stash with a git stash drop
command if you like.
在此之后,如果您愿意,您可以使用git隐藏命令删除该隐藏文件。
#2
4100
For all unstaged files use:
对于所有未分阶段的文件使用:
git checkout -- .
For a specific file use:
对于特定的文件使用:
git checkout path/to/file/to/revert
Make sure to include the period at the end.
最后一定要写上句号。
#3
1594
It seems like the complete solution is:
似乎完整的解决方案是:
git clean -df
git checkout -- .
git clean
removes all untracked files (warning: while it won't delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders) and git checkout
clears all unstaged changes.
git clean删除所有未跟踪的文件(警告:虽然它不会删除直接在.gitignore中提到的被忽略的文件,但是它可以删除文件夹中被忽略的文件),git checkout清除所有未分阶段的更改。
#4
268
This checks out the current index for the current directory, throwing away all changes in files from the current directory downwards.
这将检查当前目录的当前索引,并将文件中的所有更改从当前目录向下抛出。
git checkout .
or this which checks out all files from the index, overwriting working tree files.
或者检查索引中的所有文件,覆盖工作树文件。
git checkout-index -a -f
#5
208
git clean -df
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
通过递归地删除不在版本控制下的文件,从当前目录开始清理工作树。
-d
: Remove untracked directories in addition to untracked files
-d:除去未跟踪文件之外的未跟踪目录
-f
: Force (might be not necessary depending on clean.requireForce
setting)
-f:用力(可能不需要,取决于干净。requireForce设置)
Run git help clean
to see the manual
运行git帮助查看手册
#6
78
My favorite is
我最喜欢的是
git checkout -p
That lets you selectively revert chunks.
这允许您选择性地恢复块。
See also:
参见:
git add -p
#7
62
Since no answer suggests the exact option combination that I use, here it is:
因为没有答案表明我使用的确切的选项组合,这里是:
git clean -dfx
git checkout .
This is the online help text for the used git clean
options:
这是在线帮助文本为使用git清洁选项:
-d
- d
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f
option twice if you really want to remove such a directory.
除了未跟踪的文件之外,删除未跟踪的目录。如果未跟踪的目录由不同的Git存储库管理,则默认情况下不会删除它。如果您真的想删除这样的目录,请使用-f选项两次。
-f
- f
If the Git configuration variable clean.requireForce
is not set to false
, Git clean will refuse to delete files or directories unless given -f
, -n
, or -i
. Git will refuse to delete directories within the .git
subdirectory or file, unless a second -f
is given.
如果Git配置变量被清除。requireForce没有设置为false, Git clean将拒绝删除文件或目录,除非给出-f、-n或-i。Git将拒绝删除. Git子目录或文件中的目录,除非给出第二个-f。
-x
- x
Don’t use the ignore rules from .gitignore
(per directory) and $GIT_DIR/info/exclude
, but do still use the ignore rules given with -e
options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset
) to create a pristine working directory to test a clean build.
不要使用来自.gitignore(每个目录)和$GIT_DIR/info/ exclusion的忽略规则,但是仍然要使用带有-e选项的忽略规则。这允许删除所有未跟踪的文件,包括构建产品。可以使用它(可能与git reset一起使用)创建一个原始的工作目录来测试干净的构建。
Also, git checkout .
needs to be done in the root of the repo.
此外,git checkout。需要在回购的根上完成。
#8
49
I really found this article helpful for explaining when to use what command: http://www.szakmeister.net/blog/2011/oct/12/reverting-changes-git/
我发现这篇文章对解释什么时候使用什么命令很有帮助:http://www.szakmeister.net/blog/2011/oct/12/reverting- changgit/
There are a couple different cases:
有几个不同的案例:
-
If you haven't staged the file, then you use
git checkout
. Checkout "updates files in the working tree to match the version in the index". If the files have not been staged (aka added to the index)... this command will essentially revert the files to what your last commit was.如果没有对文件进行分段,那么使用git签出。签出“更新工作树中的文件以匹配索引中的版本”。如果文件没有分段(也就是添加到索引中)……该命令将文件还原为您上次提交的内容。
git checkout -- foo.txt
git checkout——foo.txt
-
If you have staged the file, then use git reset. Reset changes the index to match a commit.
如果已经对文件进行了分段,那么使用git reset。重置修改索引以匹配提交。
git reset -- foo.txt
git重置,foo.txt
I suspect that using git stash
is a popular choice since it's a little less dangerous. You can always go back to it if you accidently blow too much away when using git reset. Reset is recursive by default.
我怀疑使用git stab是一种流行的选择,因为它没有那么危险。如果您在使用git复位时不小心吹走了太多,您可以回到它。缺省情况下,重置是递归的。
Take a look at the article above for further advice.
看看上面的文章,寻求更多的建议。
#9
44
The easiest way to do this is by using this command:
最简单的方法是使用以下命令:
This command is used to discard changes in working directory -
此命令用于丢弃工作目录-中的更改
git checkout -- .
https://git-scm.com/docs/git-checkout
https://git-scm.com/docs/git-checkout
In git command, stashing of untracked files is achieved by using:
在git命令中,未跟踪文件的存储通过以下方式实现:
git stash -u
http://git-scm.com/docs/git-stash
http://git-scm.com/docs/git-stash
#10
39
If you aren't interested in keeping the unstaged changes (especially if the staged changes are new files), I found this handy:
如果您对保持未分阶段的更改不感兴趣(特别是在阶段更改为新文件时),我发现这很方便:
git diff | git apply --reverse
#11
38
As you type git status, (use "git checkout -- ..." to discard changes in working directory) is shown.
当您输入git状态时,(使用“git checkout -…”在工作目录中丢弃更改)将显示出来。
e.g. git checkout -- .
例如git签出——
#12
36
git checkout -f
git checkout - f
man git-checkout
:
男人git-checkout:
-f, --force
- f,力
When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.
当切换分支时,即使索引或工作树与HEAD不同,也要继续。这用于丢弃本地更改。
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.
在从索引检出路径时,不要在未合并的项上失败;相反,未合并的条目将被忽略。
#13
33
You can use git stash - if something goes wrong, you can still revert from the stash. Similar to some other answer here, but this one also removes all unstaged files and also all unstaged deletes:
您可以使用git存储—如果出现问题,您仍然可以从该存储中恢复。类似于这里的其他答案,但是这个也删除了所有的非分阶段文件和所有的非分阶段删除:
git add .
git stash
if you check that everything is OK, throw the stash away:
如果你检查一切都好,扔掉这些东西:
git stash drop
The answer from Bilal Maqsood with git clean
also worked for me, but with the stash I have more control - if I do sth accidentally, I can still get my changes back
从Bilal Maqsood和git clean的答案也为我工作,但我有更多的控制权——如果我不小心做了某件事,我仍然可以找回我的改变。
UPDATE
更新
I think there is 1 more change (don't know why this worked for me before):
我觉得还有一个变化(不知道为什么这个对我有用):
git add . -A
instead of git add .
git添加。-而不是git add。
without the -A
the removed files will not be staged
没有-A,删除的文件将不会被暂存
#14
30
Instead of discarding changes, I reset my remote to the origin. Note - this method is to completely restore your folder to that of the repo.
我没有丢弃更改,而是将遥控器重置到原点。注意-这个方法是完全恢复你的文件夹到那个回购。
So I do this to make sure they don't sit there when I git reset (later - excludes gitignores on the Origin/branchname)
所以我这样做是为了确保当我重新设置时它们不在那里(稍后——不包括在原点/分支名上的gitignores)
NOTE: If you want to keep files not yet tracked, but not in GITIGNORE you may wish to skip this step, as it will Wipe these untracked files not found on your remote repository (thanks @XtrmJosh).
注意:如果您想保持文件未被跟踪,但不在GITIGNORE中,您可能希望跳过这一步,因为它将擦除远程存储库中未被跟踪的文件(谢谢@XtrmJosh)。
git add --all
Then I
然后我
git fetch --all
Then I reset to origin
然后重置到原点
git reset --hard origin/branchname
That will put it back to square one. Just like RE-Cloning the branch, WHILE keeping all my gitignored files locally and in place.
它会回到1的平方。就像重新克隆分支一样,同时在本地和适当的地方保留我所有的gitignored文件。
Updated per user comment below: Variation to reset the to whatever current branch the user is on.
更新每个用户评论下面:变化重置到任何当前的分支用户是在。
git reset --hard @{u}
#15
22
Tried all the solutions above but still couldn't get rid of new, unstaged files.
尝试了以上所有的解决方案,但仍然无法摆脱新的、未分阶段的文件。
Use git clean -f
to remove those new files - with caution though! Note the force option.
使用git clean -f删除那些新文件——但是要小心!注意力量的选择。
#16
19
Just use:
只使用:
git stash -u
Done. Easy.
完成了。一件容易的事。
If you really care about your stash stack then you can follow with git stash drop
. But at that point you're better off using (from Mariusz Nowak):
如果您真的关心您的隐藏堆栈,那么您可以使用git隐藏删除。但这时你最好使用(来自Mariusz Nowak):
git checkout -- .
git clean -df
Nonetheless, I like git stash -u
the best because it "discards" all tracked and untracked changes in just one command. Yet git checkout -- .
only discards tracked changes, and git clean -df
only discards untracked changes... and typing both commands is far too much work :)
尽管如此,我还是喜欢git存储库,因为它“丢弃”了所有跟踪和未跟踪的更改。但是git checkout——。只丢弃跟踪的更改,而git clean -df只丢弃未跟踪的更改……而且输入这两个命令实在是太麻烦了。
#17
17
simply say
简单地说
git stash
It will remove all your local changes. You also can use later by saying
它将删除所有本地更改。后面你也可以这样说
git stash apply
or git stash pop
或git藏流行
#18
15
This works even in directories that are; outside of normal git permissions.
这甚至可以在目录中找到;在正常的git权限之外。
sudo chmod -R 664 ./* && git checkout -- . && git clean -dfx
Happened to me recently
最近发生在我身上
#19
13
cd path_to_project_folder # take you to your project folder/working directory
git checkout . # removes all unstaged changes in working directory
#20
9
Another way to get rid of new files that is more specific than git clean -df (it will allow you to get rid of some files not necessarily all), is to add the new files to the index first, then stash, then drop the stash.
另一种处理新文件的方法是先将新文件添加到索引中,然后保存,然后删除。
This technique is useful when, for some reason, you can't easily delete all of the untracked files by some ordinary mechanism (like rm).
当出于某种原因,您不能通过一些普通的机制(比如rm)删除所有未跟踪的文件时,这种技术非常有用。
#21
9
In my opinion,
在我看来,
git clean -df
should do the trick. As per Git documentation on git clean
应该足够了。根据Git清洁文档
git-clean - Remove untracked files from the working tree
清理单元——从工作树中删除未跟踪的文件
Description
描述
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选项,也会删除被忽略的文件。例如,这对于删除所有构建产品是有用的。
If any optional ... arguments are given, only those paths are affected.
如果任何可选…给出参数,只影响这些路径。
Options
选项
-d Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.
-d除去未跟踪文件之外的未跟踪目录。如果未跟踪的目录由不同的Git存储库管理,则默认情况下不会删除它。如果您真的想删除这样的目录,请使用-f选项两次。
-f --force If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to run unless given -f, -n or -i.
-f——如果Git配置变量被清除,则强制执行。没有将requireForce设置为false, git clean将拒绝运行,除非给出-f、-n或-i。
#22
8
What follows is really only a solution if you are working with a fork of a repository where you regularly synchronize (e.g. pull request) with another repo. Short answer: delete fork and refork, but read the warnings on github.
只有当您使用存储库的一个分支时(例如,提取请求),才会使用另一个repo。简短的回答:删除fork和refork,但是阅读github上的警告。
I had a similar problem, perhaps not identical, and I'm sad to say my solution is not ideal, but it is ultimately effective.
我有一个相似的问题,也许不完全相同,我很遗憾地说我的解决方案并不理想,但它最终是有效的。
I would often have git status messages like this (involving at least 2/4 files):
我经常会有这样的git状态消息(至少包含2/4个文件):
$ git status
# Not currently on any branch.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: doc/PROJECT/MEDIUM/ATS-constraint/constraint_s2var.dats
# modified: doc/PROJECT/MEDIUM/ATS-constraint/parsing/parsing_s2var.dats
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: doc/PROJECT/MEDIUM/ATS-constraint/constraint_s2Var.dats
# modified: doc/PROJECT/MEDIUM/ATS-constraint/parsing/parsing_s2Var.dats
A keen eye will note that these files have dopplegangers that are a single letter in case off. Somehow, and I have no idea what led me down this path to start with (as I was not working with these files myself from the upstream repo), I had switched these files. Try the many solutions listed on this page (and other pages) did not seem to help.
一个敏锐的眼睛会注意到,这些文件有面貌极相似的人一个字母了。不知为什么,我不知道让我走上这条路开始(我没有处理这些文件从上游回购),我不得不将这些文件。尝试在这个页面(和其他页面)列出的许多解决方案似乎没有帮助。
I was able to fix the problem by deleting my forked repository and all local repositories, and reforking. This alone was not enough; upstream had to rename the files in question to new filenames. As long as you don't have any uncommited work, no wikis, and no issues that diverge from the upstream repository, you should be just fine. Upstream may not be very happy with you, to say the least. As for my problem, it is undoubtedly a user error as I'm not that proficient with git, but the fact that it is far from easy to fix points to an issue with git as well.
通过删除我的分叉存储库和所有本地存储库并进行重新设计,我能够解决这个问题。光有这一点是不够的;上游必须将有问题的文件重命名为新的文件名。只要您没有任何未完成的工作、没有wiki,并且没有与上游存储库分离的问题,您就应该没事。退一步说,上游的人可能对你不太满意。至于我的问题,它无疑是一个用户错误,因为我并不是很精通git,但是它与git之间的问题也很不容易解决。
#23
8
No matter what state your repo is in you can always reset to any previous commit:
无论你的回购是在什么状态,你都可以重置到之前的任何承诺:
git reset --hard <commit hash>
This will discard all changes which were made after that commit.
这将丢弃在提交之后所做的所有更改。
#24
8
If you merely wish to remove changes to existing files, use checkout
(documented here).
如果您只是希望删除对现有文件的更改,请使用checkout(这里有文档说明)。
git checkout -- .
- No branch is specified, so it checks out the current branch.
- 没有指定分支,因此它检查当前分支。
- The double-hyphen (
--
) tells Git that what follows should be taken as its second argument (path), that you skipped specification of a branch. - 双连字符(-)告诉Git,以下内容应该作为第二个参数(路径),您跳过了分支的规范。
- The period (
.
) indicates all paths. - 句点(.)表示所有路径。
If you want to remove files added since your last commit, use clean
(documented here):
如果您想删除自上次提交以来添加的文件,请使用clean(这里有文档说明):
git clean -i
- The
-i
option initiates an interactiveclean
, to prevent mistaken deletions. - -i选项启动交互式清理,以防止错误删除。
- A handful of other options are available for a quicker execution; see the documentation.
- 可以使用其他一些选项来更快地执行;见文档。
If you wish to move changes to a holding space for later access, use stash
(documented here):
如果您希望将更改移动到存储空间以供以后访问,请使用隐藏(此处有文档说明):
git stash
- All changes will be moved to Git's Stash, for possible later access.
- 所有更改都将转移到Git的存储区,以便以后访问。
- A handful of options are available for more nuanced stashing; see the documentation.
- 一些选项可以用于更细微的存储;见文档。
#25
6
When you want to transfer a stash to someone else:
当你想转移一个藏匿点给别人:
# add files
git add .
# diff all the changes to a file
git diff --staged > ~/mijn-fix.diff
# remove local changes
git reset && git checkout .
# (later you can re-apply the diff:)
git apply ~/mijn-fix.diff
[edit] as commented, it ís possible to name stashes. Well, use this if you want to share your stash ;)
[编辑]正如评论所言,可以命名堆栈。如果你想分享你的收藏,就用这个吧
#26
5
If all the staged files were actually committed, then the branch can simply be reset e.g. from your GUI with about three mouse clicks: Branch, Reset, Yes!
如果所有的暂存文件都已提交,那么就可以简单地对分支进行重置,例如,从您的GUI中进行大约三次鼠标点击:branch, reset, Yes!
So what I often do in practice to revert unwanted local changes is to commit all the good stuff, and then reset the branch.
因此,在实践中,我通常要做的是恢复不需要的本地更改,即提交所有好的内容,然后重新设置分支。
If the good stuff is committed in a single commit, then you can use "amend last commit" to bring it back to being staged or unstaged if you'd ultimately like to commit it a little differently.
如果在一次提交中提交了好的内容,那么如果您最终希望提交的内容稍微有所不同,那么您可以使用“修订最后提交”将其恢复为分阶段提交或非分阶段提交。
This might not be the technical solution you are looking for to your problem, but I find it a very practical solution. It allows you to discard unstaged changes selectively, resetting the changes you don't like and keeping the ones you do.
这可能不是您正在寻找的技术解决方案,但我发现它是一个非常实用的解决方案。它允许您有选择地丢弃未分阶段的更改,重新设置您不喜欢的更改,并保留您所做的更改。
So in summary, I simply do commit, branch reset, and amend last commit.
总之,我只需要提交、分支重置和修改最后提交。
#27
5
None of the solutions work if you just changed the permissions of a file (this is on DOS/Windoze)
如果您只是更改文件的权限(这是在DOS/Windoze上),那么这些解决方案都不起作用。
Mon 23/11/2015-15:16:34.80 C:\...\work\checkout\slf4j+> git status On branch SLF4J_1.5.3 Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: .gitignore modified: LICENSE.txt modified: TODO.txt modified: codeStyle.xml modified: pom.xml modified: version.pl no changes added to commit (use "git add" and/or "git commit -a") Mon 23/11/2015-15:16:37.87 C:\...\work\checkout\slf4j+> git diff diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/TODO.txt b/TODO.txt old mode 100644 new mode 100755 diff --git a/codeStyle.xml b/codeStyle.xml old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 diff --git a/version.pl b/version.pl old mode 100644 new mode 100755 Mon 23/11/2015-15:16:45.22 C:\...\work\checkout\slf4j+> git reset --hard HEAD HEAD is now at 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore Mon 23/11/2015-15:16:47.42 C:\...\work\checkout\slf4j+> git clean -f Mon 23/11/2015-15:16:53.49 C:\...\work\checkout\slf4j+> git stash save -u Saved working directory and index state WIP on SLF4J_1.5.3: 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore HEAD is now at 8fa8488 12133-CHIXMISSINGMESSAGES MALCOLMBOEKHOFF 20141223124940 Added .gitignore Mon 23/11/2015-15:17:00.40 C:\...\work\checkout\slf4j+> git stash drop Dropped refs/stash@{0} (cb4966e9b1e9c9d8daa79ab94edc0c1442a294dd) Mon 23/11/2015-15:17:06.75 C:\...\work\checkout\slf4j+> git stash drop Dropped refs/stash@{0} (e6c49c470f433ce344e305c5b778e810625d0529) Mon 23/11/2015-15:17:08.90 C:\...\work\checkout\slf4j+> git stash drop No stash found. Mon 23/11/2015-15:17:15.21 C:\...\work\checkout\slf4j+> git checkout -- . Mon 23/11/2015-15:22:00.68 C:\...\work\checkout\slf4j+> git checkout -f -- . Mon 23/11/2015-15:22:04.53 C:\...\work\checkout\slf4j+> git status On branch SLF4J_1.5.3 Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: .gitignore modified: LICENSE.txt modified: TODO.txt modified: codeStyle.xml modified: pom.xml modified: version.pl no changes added to commit (use "git add" and/or "git commit -a") Mon 23/11/2015-15:22:13.06 C:\...\work\checkout\slf4j+> git diff diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/TODO.txt b/TODO.txt old mode 100644 new mode 100755 diff --git a/codeStyle.xml b/codeStyle.xml old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 diff --git a/version.pl b/version.pl old mode 100644 new mode 100755
The only way to fix this is to manually reset the permissions on the changed files:
唯一的解决方法是手动重置修改文件的权限:
Mon 23/11/2015-15:25:43.79 C:\...\work\checkout\slf4j+> git status -s | egrep "^ M" | cut -c4- | for /f "usebackq tokens=* delims=" %A in (`more`) do chmod 644 %~A Mon 23/11/2015-15:25:55.37 C:\...\work\checkout\slf4j+> git status On branch SLF4J_1.5.3 nothing to commit, working directory clean Mon 23/11/2015-15:25:59.28 C:\...\work\checkout\slf4j+> Mon 23/11/2015-15:26:31.12 C:\...\work\checkout\slf4j+> git diff
#28
5
You could create your own alias which describes how to do it in a descriptive way.
您可以创建您自己的别名,该别名描述如何以一种描述性的方式进行此操作。
I use the next alias to discard changes.
我使用下一个别名来丢弃更改。
Discard changes in a (list of) file(s) in working tree
discard = checkout --
Then you can use it as next to discard all changes:
然后你可以使用它作为下一个放弃所有的改变:
discard .
Or just a file:
或者只是一个文件:
discard filename
Otherwise, if you want to discard all changes and also the untracked files, I use a mix of checkout and clean:
否则,如果您想丢弃所有更改和未跟踪的文件,我将使用校验和清理的混合:
Clean and discard changes and untracked files in working tree
cleanout = !git clean -df && git checkout -- .
So the use is simple as next:
因此,下面的用法很简单:
cleanout
Now is available in the next Github repo which contains a lot of aliases:
现在可以在下一个Github repo中找到,它包含了很多别名:
- https://github.com/GitAlias/gitalias
- https://github.com/GitAlias/gitalias
#29
5
If you are in case of submodule and no other solutions work try:
如果您是子模块,没有其他解决方案,请尝试:
-
To check what is the problem (maybe a "dirty" case) use:
检查问题是什么(可能是“脏”的情况)使用:
git diff
git diff
-
To remove stash
删除隐藏
git submodule update
git子模块更新
#30
4
I had a weird situation where a file is always unstaged, this helps me to resolve.
我有一个奇怪的情况,文件总是未分阶段,这有助于我解决问题。
git rm .gitattributes
git add -A
git reset --hardgit rm .gitattributes git添加-一个git复位——很难