git: Your branch and 'origin/master' have diverged - how to throw away local commits?

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I have the following message in 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.

(Merging is going to be too difficult, I'd rather make the 3 local commits again once master is up to date.)

How can I do this?

回答1:

git fetch origin git reset --hard origin/master


回答2:

To preserve your old commits on a temporary branch in case you need them:

git branch temp

Then switch to the new master

git fetch origin git reset --hard origin/master


回答3:

Try doing this to blow away your local commits:

git reset --hard HEAD~4


回答4:

As an alternative to merging, you can rebase the feature branch onto master branch using the following commands:

git checkout feature git rebase master


回答5:

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  git checkout -b  origin/

Using master as an example:

git branch -D master git checkout -b master origin/master


回答6:

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 before pulling from the remote repository.

To be able to pull without conflicts use git stash, and then git pull.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!