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.

  • How can I corrupt a Git repository?
  • Find out where a GitHub repo was forked from
  • What does Egit mean with the number in parenthesis in Package Explorer
  • git subtree or submodule for surrounding directories
  • Does the case for not including Node modules in version control also apply to Composer packages?
  • git says untracked content and cant add it to repo
  • 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

  • Git - fatal: Unable to create '/path/my_project/.git/index.lock': File exists
  • Difference between `git branch -f <branch_name> <hash>` and `git checkout <branch_name>; git reset --hard <hash>` under a clean working tree?
  • Make a branch undeletable in GitHub
  • Why is merging feature branches into release branches a bad idea?
  • how to sync repos of bitbucket on github?
  • Git: apply topic branch using rebase (without merge)
  • 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.