如何丢弃 Git 中未暂存的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:44:59  来源:igfitidea点击:

How do I discard unstaged changes in Git?

gitversion-control

提问by Readonly

How do I discard changes in my working copy that are not in the index?

如何丢弃索引中没有的工作副本中的更改?

采纳答案by Greg Hewgill

Another quicker way is:

另一种更快的方法是:

git stash save --keep-index --include-untracked

You don't need to include --include-untrackedif you don't want to be thorough about it.

--include-untracked如果您不想彻底了解它,则不需要包括在内。

After that, you can drop that stash with a git stash dropcommand if you like.

之后,git stash drop如果您愿意,您可以使用命令删除该存储。

回答by Tobi

For all unstaged files in current working directory use:

对于当前工作目录中的所有未暂存文件,请使用:

git checkout -- .

For a specific file use:

对于特定文件使用:

git checkout -- path/to/file/to/revert

--here to remove argument ambiguation.

--在这里删除参数歧义

回答by Mariusz Nowak

It seems like the complete solution is:

似乎完整的解决方案是:

git clean -df
git checkout -- .

git cleanremoves 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 checkoutclears all unstaged changes.

git clean删除所有未跟踪的文件(警告:虽然它不会删除 .gitignore 中直接提到的忽略文件,但它可能会删除驻留在文件夹中的忽略文件)并git checkout清除所有未暂存的更改。

回答by CB Bailey

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

回答by Elvis Ciotti

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.requireForcesetting)

-f:强制(根据clean.requireForce设置可能不需要 )

Run git help cleanto see the manual

跑去git help clean看说明书

回答by Ben

My favorite is

我最喜欢的是

git checkout -p

That lets you selectively revert chunks.

这使您可以有选择地还原块。

See also:

也可以看看:

git add -p

回答by Martin G

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 cleanoptions:

这是所用git clean选项的在线帮助文​​本:

-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 -foption twice if you really want to remove such a directory.

除未跟踪的文件外,还删除未跟踪的目录。如果未跟踪的目录由不同的 Git 存储库管理,则默认情况下不会将其删除。-f如果您真的想删除这样的目录,请使用选项两次。

-f

-f

If the Git configuration variable clean.requireForceis 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 .gitsubdirectory or file, unless a second -fis given.

如果Git的配置变量clean.requireForce未设置为false,Git的清洁会拒绝,除非给予删除文件或目录-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 -eoptions. 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/exclude的忽略规则,但仍然使用-e选项给出的忽略规则。这允许删除所有未跟踪的文件,包括构建产品。这可用于(可能与 结合使用git reset)来创建原始工作目录以测试干净的构建。

Also, git checkout .needs to be done in the root of the repo.

此外,git checkout .需要在 repo 的根目录中完成。

回答by 2540625

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.
  • The period (.) indicates all paths.
  • 没有指定分支,所以它检查当前分支。
  • 双连字符 ( --) 告诉 Git 后面的内容应该作为它的第二个参数(路径),你跳过了分支的规范。
  • 句点 ( .) 表示所有路径。

If you want to remove files addedsince your last commit, use clean(documented here):

如果要删除自上次提交以来添加的文件,请使用clean此处记录):

git clean -i 
  • The -ioption initiates an interactive clean, to prevent mistaken deletions.
  • A handful of other options are available for a quicker execution; see the documentation.
  • -i选项启动交互clean,以防止错误删除。
  • 一些其他选项可用于更快地执行;请参阅文档。

If you wish to move changes to a holding space for later access, use stash(documented here):

如果您希望将更改移动到保留空间以供以后访问,请使用stash此处记录):

git stash
  • All changes will be moved to Git's Stash, for possible later access.
  • A handful of options are available for more nuanced stashing; see the documentation.
  • 所有更改都将移至 Git 的 Stash,以供以后访问。
  • 一些选项可用于更细致的存储;请参阅文档。

回答by blak3r

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-changes-git/

There are a couple different cases:

有几种不同的情况:

  1. 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 checkout -- foo.txt

  2. If you have staged the file, then use git reset. Reset changes the index to match a commit.

    git reset -- foo.txt

  1. 如果您尚未暂存文件,则使用git checkout. 签出“更新工作树中的文件以匹配索引中的版本”。如果文件尚未暂存(也称为添加到索引中)...此命令基本上会将文件还原为您上次提交的内容。

    git checkout -- foo.txt

  2. 如果您已暂存文件,请使用 git reset。重置更改索引以匹配提交。

    git reset -- foo.txt

I suspect that using git stashis 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 stash是一种流行的选择,因为它的危险性较小。如果您在使用 git reset 时不小心吹得太多,您可以随时返回它。默认情况下,重置是递归的。

Take a look at the article above for further advice.

请查看上面的文章以获得进一步的建议。

回答by A H M Forhadul Islam

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