git:你的分支和'origin/master'有分歧 - 如何丢弃本地提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19864934/
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
git: Your branch and 'origin/master' have diverged - how to throw away local commits?
提问by Richard
I have the following message in git:
我在 git 中有以下消息:
# Your branch and 'origin/master' have diverged,
# and have 3 and 8 different commits each, respectively.
# (use "git pull" to merge the remote branch into yours)
I would like to throw away the 3 local commits, and pull the 8 remote commits at origin/master.
我想扔掉 3 个本地提交,并在 origin/master 上拉取 8 个远程提交。
(Merging is going to be too difficult, I'd rather make the 3 local commits again once master is up to date.)
(合并将太困难,一旦 master 是最新的,我宁愿再次进行 3 个本地提交。)
How can I do this?
我怎样才能做到这一点?
回答by SLaks
git fetch origin
git reset --hard origin/master
回答by jdramer
To preserve your old commits on a temporary branch in case you need them:
在临时分支上保留旧提交以备不时之需:
git branch temp
Then switch to the new master
然后切换到新的master
git fetch origin
git reset --hard origin/master
回答by manojlds
Try doing this to blow away your local commits:
尝试这样做以消除您的本地提交:
git reset --hard HEAD~4
回答by Josip Ivic
As an alternative to merging, you can rebase the feature branch onto master branch using the following commands:
作为合并的替代方法,您可以使用以下命令将功能分支重新定位到主分支上:
git checkout feature
git rebase master
回答by Gemtastic
If a hard reset doesn't cut it for you and you don't want to do a pull-merge you can throw away your local unwanted changes by deleting your local branch and re-download the origin one:
如果硬重置不适合您并且您不想进行合并,则可以通过删除本地分支并重新下载原始分支来丢弃本地不需要的更改:
git branch -D <local branch>
git checkout -b <branch name> origin/<branch name>
Using master
as an example:
使用master
作为一个例子:
git branch -D master
git checkout -b master origin/master
回答by Bremsstrahlung
To erase your latest local commit use the following:
要删除最新的本地提交,请使用以下命令:
git reset HEAD^
This will get the header back to the state previous to the commit, and will allow you to git pull
from master. Make sure you save all your changes elsewhere (locally) before pulling from the remote repository.
这将使标头恢复到提交前的状态,并允许您git pull
从 master 返回。在从远程存储库中提取之前,请确保将所有更改保存在其他地方(本地)。
To be able to pull without conflicts use git stash
, and then git pull
.
为了能够拉而不冲突使用git stash
,然后git pull
。