2

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:

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
Community
  • 1
  • 1
John Smith
  • 363
  • 3
  • 13
  • Which tree is the cleaned-up one that you want to keep? Did you draw these graphs manually? just asking as the 'remote/feature/test-feature' suggests as there was a remote called 'feature'.. – inger Jan 30 '13 at 20:57
  • Sorry, that should have been 'remote/origin/feature/test-feature'. The plots were drawn manually but both trees show up when I do `gitk --all`. I redrew the image so it looks a bit more like my situation. The top tree was the one I wanted to keep and the bottom one had the "old" repository history. – John Smith Jan 31 '13 at 21:43

2 Answers2

1

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.

inger
  • 19,574
  • 9
  • 49
  • 54
  • I'd say it's the latter. If I had access to the remote, I would delete the bare repository and push my local one; however, I don't have file access (only https). So I need to overwrite but I want to push the entire repository with all my branches. Some branches are the same name so I guess it tried to merge where it could and kept a detached history where it couldn't. – John Smith Jan 30 '13 at 19:09
0

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>
    
John Smith
  • 363
  • 3
  • 13