23

The remote repository contains a bad version. I have the copy of a good version locally. I want to replace everything that's on the remote repository with my current repository (the remote repository has a few more commits (including a merge) that should not have happened).

If I try to push, it tells me I need to pull first, but I don't want to do that, because that would mess my local version. I want the remote repo to contain what I have locally.

How can I do that?

rid
  • 61,078
  • 31
  • 152
  • 193
  • Possible duplicate of [Force "git push" to overwrite remote files](https://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-files) – Liam Dec 12 '18 at 13:16

2 Answers2

70

Use the --force, Luke.

http://www.kernel.org/pub/software/scm/git/docs/git-push.html

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Same thing: `! [remote rejected] master -> master (non-fast-forward)` – rid Jun 10 '11 at 17:53
  • 1
    Then you need to turn off `receive.denyNonFastForwards` in the remote's repository .git/config, if it's refusing it even with `--force` specified. – Amber Jun 10 '11 at 17:54
  • Used `git config receive.denyNonFastForwards false` on the remote repo and I have the same error. – rid Jun 10 '11 at 18:00
  • @Amber, I'm running `git push -f origin master` on the local repo. – rid Jun 10 '11 at 18:00
  • Huh. Um, I guess you could try going the other way, using `git fetch` from the remote repo to pluck the good version out of your local? – Amber Jun 10 '11 at 18:04
  • That should have overwritten master on the remote then, and thus the remote should now be pointing at the good version. – Amber Jun 10 '11 at 18:22
  • I tried `git push origin some_branch --force` but it always returned Everything is up-to-date message. But, `git push origin your_branch:some_branch --force` and this was what I was missing. Hope it helps! – Akhil Jun 30 '22 at 18:45
3
  1. Make a new local branch from your known good version
  2. Pull
  3. Switch to the known bad branch
  4. Fully merge your known good branch into the known bad branch
  5. Commit and push

I usually use a process like this to preserve exactly what changed, have an isolated branch that's a known good copy, etc. It's probably excessive compared to using --force, but I prefer it.

David Fells
  • 6,678
  • 1
  • 22
  • 34
  • 2
    The `merge` doesn't work, because the good changes are older than the bad changes, so nothing to merge. – rid Jun 10 '11 at 17:56