46

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).

5
  • @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. Commented Jun 9, 2015 at 10:34
  • @MarkRotteveel I don't know, i think closing something as a duplicate retrospectively isn't logical.
    – Matt
    Commented Jun 9, 2015 at 10:36
  • @Matt This is the guidance: meta.stackexchange.com/questions/147643/… Commented Jun 9, 2015 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
    Commented Jun 9, 2015 at 10:39
  • Also could possibly be seen as duplicate of Reset local repository branch to be just like remote repository HEAD Commented Mar 31, 2020 at 8:11

2 Answers 2

67
git fetch
git reset --hard origin/master
2
  • 4
    After git fetch, of course.
    – Catherine
    Commented Aug 1, 2011 at 12:22
  • 6
    Note, this only works for a clean branch. Any changes or new commits in the local working copy will be lost. I've added an answer using rebase. Commented Jan 20, 2014 at 18:57
19

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
2
  • 13
    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
    Commented Aug 29, 2014 at 9: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.
    Commented Jun 9, 2020 at 15:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.