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.
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:
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
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.
http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html
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!
If the remote repo was removed
$ git remote add <same remote name> <url to the repo>
Remove old remote branches that aren't in the "new" repository
$ git push <same remote name> --delete <branch name>
上一篇: 删除不再存在的远程分支
下一篇: Git远程覆盖导致两个断开连接的历史记录