Git remote overwrite led to two disconnected histories

I’m pretty new to Git so I was playing around with it before I knew what workflow and structure I wanted to use for my development repository. I created a local repository that I wanted to use to replace a remote one. I am currently the only one using it (though I cloned on different machines).

Basically, I created a new local repository and got it the way I wanted it. I forced a push to the remote repository to overwrite the old one like stated here:
How can I choose to overwrite remote repository with local commits? … well except that I used the URL instead of origin.

  • Eclipse + Git - How to show only modified files in compare with option?
  • How can I connect to my own Git server in SourceTree?
  • Add subproject as usual folder to repository
  • Git push to Azure failing (RPC failed)
  • How to set the receive.denyNonFastForwards on a repository in GitHub
  • git format-patch X..Y for a specific author
  • Then I added the remote repository and checked to see if it worked.

    $ git remote add origin <url>

    I looked at the full history and I ended up with something like this:

    gitk --all

    Oh no! So I removed the remote and no change. Now I have this detached history AND branches that don’t exist. So then I tried this: How do you remove an invalid remote branch reference from Git?

    It didn’t help.

    $ git remote show

    Shows up empty. Is there any way to undo this? I don’t see how just adding a remote could muck up my working directory this badly!

    Edit: I just noticed that there is one branch on the old history that wasn’t the same as a new one. Also, I did fetch after adding the remote repository. I can see the old branch in

    $ git reflog --all

    and it shows up as

    16903f4 refs/remotes/origin/feature/test-feature@{0}: fetch origin: storing head

  • Jenkins GitHub Plugin Inverse Branches
  • How does git flow handle hotfix to older release or point release of older release
  • How can I merge two commits in git?
  • Tagging commit messages and changesets
  • How to create and apply properly .patch file to a single .cpp file using diff?
  • How to automatically pull the version number?
  • 2 Solutions collect form web for “Git remote overwrite led to two disconnected histories”

    Do you want to merge the local history with the remote one, or simply keep one of them?

    a) if you want to merge them, the 2 histories need to be connected first. Then rebase with --root is you friend I guess.

    –root Rebase all commits reachable from , instead of limiting them with an . This allows you to rebase the root commit(s)
    on a branch. Must be used with –onto, and will skip changes already
    contained in (instead of ). When used together
    with –preserve-merges, all root commits will be rewritten to have
    as parent instead.

    b) if you want to keep, say, only the local changes and drop the remote ones: just delete the remote ref:

     git push origin :badbranch

    Let me know if you need more clarification.

    I fixed it by adding the remote with the same name and deleting the remote branches that weren’t similar. I think a better solution would be one without manually deleting branches (I already have the repository and history I want to use) but at least my problem is fixed!

    1. If the remote repo was removed

      $ git remote add <same remote name> <url to the repo>
    2. Remove old remote branches that aren’t in the “new” repository

      $ git push <same remote name> --delete <branch name>
    Git Baby is a git and github fan, let's start git clone.