131

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?

starball
  • 20,030
  • 7
  • 43
  • 238
Richard
  • 62,943
  • 126
  • 334
  • 542

6 Answers6

366
git fetch origin
git reset --hard origin/master

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

starball
  • 20,030
  • 7
  • 43
  • 238
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 6
    can you do `git reset origin/master` without `--hard` so that working directory changes are not lost? – wisbucky Jun 22 '16 at 01:38
  • 1
    I see a similar message but for non-master branch when I rebase it to latest master. What would a pull do in that case? Should I pull? I want my commit to be on the latest changes. – Rajeev Ranjan Feb 18 '20 at 13:51
  • @RajeevRanjan: Pull will (attempt to) merge in your existing changes. Reset will throw them away. – SLaks Feb 18 '20 at 14:52
  • these 2 commands worked straight away. Only extra thing is, I need to restore back the changes I did previously from backup I took. Cheers! – Suresh Oct 22 '21 at 08:36
43

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
jdramer
  • 895
  • 8
  • 9
  • So in this case does he branch his branch ? If for example he was working in 'mydevbranch' then does branch temp this simply creates a new copy branch of mydevbranch ? It seems like a precautionary measure the way you put it incase they are ever needed they are in this copy.. – landed Apr 23 '14 at 06:43
  • 2
    Yes, this is a precautionary measure. It just ensures that the commits are not lost should you need them in the future. – jdramer Jun 09 '14 at 21:34
  • intuitively I would have thought that you would need to comit your local code before doing this - and thats the problem because you dont want what you just did to go upstream. I guess you comit local and dont push - but eventually it would need to go up...thats unwanted I am assuming. A branch is for a variation or different feature but it needs to happen from a comitted state ? Or maybe thats what you are saying. Perhaps I need to NOT worry about comitting - I just thought branch takes a copy from the comitted source origin (loosing local changes) – landed May 11 '15 at 08:20
  • @landed: yes, you'd commit your local changes after making branch `temp`. You can later, if you want to recover some of those commits, cherry-pick them (see `git help cherry-pick`). – Cris Luengo Sep 25 '17 at 21:07
  • 1
    Thanks, after git reset hard, then `merge temp` solved my problem – weiliang Apr 13 '21 at 12:16
10

Try doing this to blow away your local commits:

git reset --hard HEAD~4
manojlds
  • 290,304
  • 63
  • 469
  • 417
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
Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
4

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
Gemtastic
  • 6,253
  • 6
  • 34
  • 53
4

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.

Bremsstrahlung
  • 686
  • 1
  • 9
  • 23