如何丢弃 git 本地分支更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7846699/
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 12:07:51  来源:igfitidea点击:

how to discard git local branch changes?

git

提问by Jane_Meng

how to discard git local branch changes? eg, local branch with version: A->B->C Now I am on version A, and it has some changes conflict with latest version C. I want to discard local changes and pull the latest version C.

如何丢弃 git 本地分支更改?例如,版本为 A->B->C 的本地分支现在我在版本 A 上,它与最新版本 C 有一些更改冲突。我想放弃本地更改并拉取最新版本 C。

$ git pull

I will meet some error. and there are many files, so I don't need to do many times $ git co files

我会遇到一些错误。并且有很多文件,所以我不需要做很多次$ git co files

Is there any better way?

有没有更好的办法?

回答by ayoy

If you have uncommitted changes that you want to discard, use this:

如果您有要放弃的未提交更改,请使用以下命令:

$ git reset --hard

which is equivalent to

这相当于

$ git reset --hard HEAD

This removes all the local uncommitted changes. If you want to remove some offending commits from your local branch, try rewinding it:

这将删除所有本地未提交的更改。如果您想从本地分支中删除一些有问题的提交,请尝试回滚它:

$ git reset --hard HEAD^ #moves HEAD back by one commit

or e.g.

或例如

$ git reset --hard HEAD~3 #moves HEAD back by 3 commits

Use these with caution, as you won't be able to undo these operations. Once you're done cleaning up your local branch, use git pullto get the latest code.

请谨慎使用这些操作,因为您将无法撤消这些操作。清理完本地分支后,请使用git pull获取最新代码。

回答by Onlyjob

git fetch
git reset --hard origin/master

回答by daja77

Have you already commited your local changes? If not a git reset --hard HEAD should do the trick

您是否已经提交了本地更改?如果不是 git reset --hard HEAD 应该可以解决问题