45

Sometimes, esp. when I'm the only one working on a remote repository, I like rewriting the history with git rebase -i and git push origin master -f.

How do I do a forced git pull origin master without a merge? I tried it with the -f option, but that didn't work. I just want to rewrite the history of my local git repo to match that of the remote (origin).

nwalke
  • 3,170
  • 6
  • 35
  • 60
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • @Matt Age is not (always) relevant for deciding on duplicates, and as the linked question has more upvotes, and more detailed answers it is a good candidate. – Mark Rotteveel Jun 09 '15 at 10:34
  • @MarkRotteveel I don't know, i think closing something as a duplicate retrospectively isn't logical. – Matt Jun 09 '15 at 10:36
  • @Matt This is the guidance: http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha/147651#147651 – Mark Rotteveel Jun 09 '15 at 10:37
  • @MarkRotteveel Thanks i'll have a deeper read of that, but just glancing at the MOD answer i can see i was mistaken and this is the correct action to mark it as a duplicate and i stand corrected.... and marked as dupe – Matt Jun 09 '15 at 10:39
  • Also could possibly be seen as duplicate of [Reset local repository branch to be just like remote repository HEAD](https://stackoverflow.com/q/1628088/1148030) – Peter Lamberg Mar 31 '20 at 08:11

2 Answers2

66
git fetch
git reset --hard origin/master
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
18

With respect it is a few years old, the answer by MattDiPasquale will destroy any local changes or commits.

If you have local changes or commits, but need to rewrite history, run:

git fetch origin
git rebase origin/master
Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • 12
    If all you need to preserve is your current unstaged work (not local commits) just type `git stash && git fetch origin && git reset --hard origin/master && git stash pop` – novalore Aug 29 '14 at 09:12
  • Will that work if the old origin/master and your local origin master have no common root (e.g. origin history was rewritten changing all commits' SHA)? – Dan M. Jun 09 '20 at 15:58