Richard
Richard

Reputation: 65510

How can I throw away local git commits when getting "Your branch and 'origin/master' have diverged"?

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?

Upvotes: 133

Views: 167689

Answers (6)

SLaks
SLaks

Reputation: 887275

git fetch origin
git reset --hard origin/master

Note that any non-pushed commits or local changes will be lost.

Upvotes: 369

Gemtastic
Gemtastic

Reputation: 6433

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 main as an example:

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

Upvotes: 4

Bremsstrahlung
Bremsstrahlung

Reputation: 730

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.

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

Upvotes: 4

Josip Ivic
Josip Ivic

Reputation: 3709

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

git checkout feature
git rebase master

Upvotes: 4

jdramer
jdramer

Reputation: 945

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

Upvotes: 44

manojlds
manojlds

Reputation: 301047

Try doing this to blow away your local commits:

git reset --hard HEAD~4

Upvotes: 10

Related Questions