2

I cloned a repo from bitbucket quite a while back, I want to create a new repo on my github account from the current version I have. But when I try to create a new repo, I get an error stating that this directory is already a git folder. Im using github desktop. How can I create a new repo from this?

5 Answers5

1

Take a look in yout .git/config file. There is a part that explain where your remote are:

[remote "origin"]
    url = git@github.com:sensorario/a-github-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

If you want to add new remote

[remote "github"]
    url = git@github.com:sensorario/a-github-repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "bitbucket"]
    url = git@github.com:sensorario/a-bitbucket-remote.git
    fetch = +refs/heads/*:refs/remotes/origin/*

And just push

git push github master

When you create new repository in github or bitbucket, you just have a "bare repository". Here the difference between bare and non bare repository;

So, .. what you have to do is just:

  • create repository on github.com
  • add new remote in your configuration
  • push into one or other bare repository
sensorario
  • 20,262
  • 30
  • 97
  • 159
  • Thanks, exactly what I was looking for –  Apr 01 '18 at 11:03
  • Erik, HC Liu and myself have provided similar answers that meet your need but using the CLI as opposed editing configs. Glad you found your answer though. – Peter Reid Apr 01 '18 at 11:15
0

So your problem here is that your local git repo is in fact a completely separate entity from your GitHub and BitBucket remotes.

Set up your GitHub repo on github.com, then take the SSH/HTTPS URL.

Then navigate to your local repository and execute

git remote add github https://github.com/user/repo.git

(Replace https://github.com/user/repo.git with your SSH or HTTPS URL)

then

git push github --all

to push all your local branches to github


If you want to stop using your BitBucket repo and only use GitHub going forward you can do in place of the git remote add command:

 git remote set-url origin https://github.com/user/repo.git

(Replace https://github.com/user/repo.git with your SSH or HTTPS URL)

Peter Reid
  • 5,139
  • 2
  • 37
  • 33
0

One option would be to create the repository by hand on github.com. Then copy the repo's URL and run git remote add github url in your shell of choice in the root of your local repository, where url is the GitHub repo URL you copied. This adds a new remote repository called github, so your machine's git now knows there are two remote repositories

Now you can choose to which remote repository to push or pull from. The BitBucket one probably is named origin. You can verify this by running git remote -v. If you want to push a local branch to github, then check out that branch and do git push github.

If github doesn't know about that branch yet, then use git push --set-upstream github branch-name once to create the new branch remotely.

I don't use GitHub Desktop, but I assume that after you've done the aforementioned steps and then open the root directory in GitHub Desktop, then it will work. Maybe, though, GitHub Desktop assumes that GitHub's remote repository is named origin locally. If so, you can rename origin to bitbucket and github to origin.

Erik
  • 4,305
  • 3
  • 36
  • 54
0

I think "repo" you want to create is a concept only refer to the remote server. Since you have cloned a repository to local, you need not to recreate new one locally.

Firstly, log in github and create a new remote repository in browser. Let's assume that your github username is "foo" and you have created a new remote repository named "bar".

open the terminal, change the directory to your local git project folder and type

git remote add gitrepo https://github.com/foo/bar

then (if you're in master)

git push gitrepo master 

that's all.

H.C.Liu
  • 156
  • 1
  • 4
-1

If you don't want to keep git history:

  1. Go to https://github.com/new (login if needed)
  2. Create a new repo with your desired name
  3. Clone it to your computer (git clone ...)
  4. Copy/move the content of your bitbucket repo folder to this new github repo folder, but without the git files (.git folder)
  5. Add everything (git add .)
  6. Commit it (git commit -m 'Your commit message')
  7. Push it to github (git push)

Edit:
Based on comments it seems maybe OP doesn't want a new repo but just wants to move this repo to another service and preserve history. In that case:

If you want to keep git history:

  1. Go to https://github.com/new (login if needed)
  2. Create a new repo with your desired name
  3. Clone it to your computer (git clone ...)
  4. Change the origin's url (git remote set-url origin [your_github_repo_url] (more info: https://help.github.com/articles/changing-a-remote-s-url/)
Joe Samanek
  • 1,644
  • 12
  • 16
  • Whoever put the downvote, perhaps explain what the problem is for you, or else I can't help. – Joe Samanek Apr 01 '18 at 10:46
  • This solution will not retain the commit history of the repository, only the source code. – Peter Reid Apr 01 '18 at 10:50
  • It's not my downvote, but I think that copying/moving files from one local directory to another is not a great solution. Especially since we're talking about version control using `git`: it has features to add multiple remote repositories, so one local repository directory can track / push / pull multiple remote repositories. I assume your downvoter had this or a similar reason. – Erik Apr 01 '18 at 10:51
  • @PeterReid yes that was the point, no? I thought that was what OP wanted. – Joe Samanek Apr 01 '18 at 10:53
  • Also not my downvote but Erik's reasoning is pretty spot on – Peter Reid Apr 01 '18 at 10:53
  • Sure, we can add another upstream, but I understood OP specifically wanted to create a new repo, not just copy this repo to another service. If that is not the case, then yes, new upstream is of course better. – Joe Samanek Apr 01 '18 at 10:56