46

Basically, due to events beyond my control, my remote repo was moved - I did a lot of work on my local copy in the meantime and now I really just want to overwrite everything in the remote repo with my local files.

However, I don't seem to be able to do this. The closest I can get is to pull and merge, but then it wants to walk me through some convoluted process for merging. I don't want to merge. I want to overwrite. I don't need a new branch - basically, I just want a fresh start.

The remote repo is on unfuddle.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
ericgr
  • 829
  • 2
  • 9
  • 13
  • Possible duplicate of [Force "git push" to overwrite remote files](http://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-files) – gnsb Dec 10 '16 at 22:54

5 Answers5

64

You can remove the branch and recreate it, let's say the branch that you want to overwrite is dev:

Remove the branch in your remote host(github)

git push origin :dev  

Then just push your dev again:

git push origin dev

I use Github for hosting, not familiar with unfuddle, but I think it'll works for the unfuddle, too. :)


Just as @melee mentioned, you can also use

git push origin dev -f

(not sure whether the -f is valid, but --force is OK)

git push origin dev --force

to force overwrite the branch. I remember I did it before. Thanks @melee. :)

Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • 6
    I think (and I'm not sure here) you could achieve the same result by doing `git push origin dev -f` – Nic Dec 05 '11 at 15:12
  • @melee yeah, it may not the only way to do so. I just like use `:` to remove the branch. :D – Kjuly Dec 05 '11 at 15:14
  • Thanks, guys. I had tried to use --force, but that didn't work. I had already figured out another way to achieve this by the time you wrote so didn't get to try your solution. Thank you, though. – ericgr Dec 05 '11 at 15:22
  • 3
    In the end, I didn't solve this using git commands, per se. Instead, I just copied my local files to a temp directory, and then deleted my local files. then 'pull'ed down the out-dated files from the remote repo into the now-empty local folder. Then I copied over those out of date files from the temp directory, did a commit and then a push back to the repo. That at least solved my immediate problem and now I am back on track with local and remote repo being in sync. – ericgr Dec 05 '11 at 15:25
  • 6
    @ericgr you should add that as an answer! – Nic Dec 05 '11 at 18:52
  • 2
    ^ Except you lose all git history, which surely isn't ideal. – Carson Nov 17 '14 at 15:15
  • @Carson yeah, but that's what the OP want ;) – Kjuly Nov 18 '14 at 01:10
  • 3
    `-f` and `--force` are synonyms. – bzeaman Feb 10 '17 at 13:47
  • If I do this for the `master`branch (`git push origin master -f`), git does a `Writing objects`, then it seems all done, because a subsequent `git push origin master` just gives `Everything up-to-date`. I suppose it's fine then. – David Tonhofer Aug 25 '18 at 11:35
  • @DavidTonhofer right, it should be fine. However, personally, I don't suggest force updating master br, which suppose to be a stable branch. It's a good idea to use `git merge --squash` whenever merge new feature br to dev or master, so whenever sth happens unexpectedly, u can just revert that commit. – Kjuly Aug 26 '18 at 08:03
14

Been having the same problem. This command worked for me.

git push --set-upstream origin master --force
Joey Phillips
  • 1,543
  • 2
  • 14
  • 22
  • Very Good solution, clean! BUT WARNING! DO IT ONLY ON SOLO, OR WITH AGREEMENTS OF WHOLE TEAM. Very very dangerous for a fully collaborative repos. – KeitelDOG Jul 16 '22 at 20:42
5

How to do this for master branch, without pulling data down from the remote repo:

  1. Create a new folder, init git, add remote repo - don't pull or fetch!

    mkdir clean_repo

    git init

    git remote add origin <remote-repo>

  2. create (and switch to) empty local branch, add, commit and push a test file into this.

    git checkout test

    echo "test" > test

    git add .

    git commit -m "adding test"

    git push origin:test

  3. On github / bitbucket, change default branch to new branch

  4. On local, switch to master branch, commit and push to remote repo / branch

    git checkout -b master

    git push origin --mirror

a20
  • 5,495
  • 2
  • 30
  • 27
2

Add remote repo if hadn't:

git remote add <remote-name> <remote-repo>

git push --set-upstream <remote-name> <branch-name> --force
Oybek Odilov
  • 185
  • 9
0

I'm not an expert in github ecosystem, but why can't you just reset your remote repository url?

git remote set-url origin /path/to/your/new/remote/repository/url

You might also need to configure your up-stream branch by looking in here.

for more on git remote..., please take a look here.

Simple-Solution
  • 4,209
  • 12
  • 47
  • 66