135

Is there a way to import my local git repos to SourceTree and push them to my Bitbucket account, having new repos identical to my local repos be created on my account? Or do I have to make a repo online first and push to that? Github has a way to publish your local repos directly from it's client, and it creates it automatically when you publish, but it has limited private repos. I'm just doing homework so there's no reason for it to be public, hence why I'm trying to use bitbucket.

cma0014
  • 1,517
  • 3
  • 11
  • 9

11 Answers11

170

(updated on 3-29-2019 to use the https instead of ssh, so you don't need to use ssh keys)

It seems like for BitBucket, you do have to create a repo online first. Using the instructions from Atlassian, simply create a new BitBucket repository, copy the repository url to the clipboard, and then add that repository as a new remote to your local repository (full steps below):

Get Repo URL

  1. in your BitBucket repo, choose "Clone" on the top-right
  2. choose "HTTPS" instead of "SSH" in the top-right of the dialog
  3. it should show your repo url in the form git clone <repository url>

Add Remote Using CLI

  1. cd /path/to/my/repo
  2. git remote add origin https://bitbucket.org/<username>/<reponame>.git
  3. git push -u origin --all

Add Remote Using SourceTree

  1. Repository>Add Remote...
  2. Paste the BitBucket repository url (https://bitbucket.org/<username>/<reponame>.git)

Old Method: Creating & Registering SSH Keys

(this method is if you use the ssh url instead of the https url, which looks like ssh://git@bitbucket.org/<username>/<reponame>.git. I recommend just using https)

BitBucket is great for private repos, but you'll need to set up an ssh key to authorize your computer to work with your BitBucket account. Luckily Sourcetree makes it relatively simple:

Creating a Key In SourceTree:

  1. In Tools>Options, make sure SSH Client: is set to PuTTY/Plink under the General tab
  2. Select Tools>Create or Import SSH Keys
  3. In the popup window, click Generate and move your mouse around to give randomness to the key generator
  4. You should get something like whats shown in the screenshot below. Copy the public key (highlighted in blue) to your clipboard

    putty

  5. Click Save private Key and Save public key to save your keys to wherever you choose (e.g. to <Home Dir>/putty/ssk-key.ppk and <Home Dir>/putty/ssh-key.pub respectively) before moving on to the next section

Registering The Key In BitBucket

  1. Log in to your BitBucket account, and on the top right, click your profile picture and click Settings
  2. Go to the SSH Keys tab on the left sidebar
  3. Click Add SSH Key, give it a name, and paste the public key you copied in step 4 of the previous section

That's it! You should now be able to push/pull to your BitBucket private repos. Your keys aren't just for Git either, many services use ssh keys to identify users, and the best part is you only need one. If you ever lose your keys (e.g. when changing computers), just follow the steps to create and register a new one.

Sidenote: Creating SSH Keys using CLI

Just follow this tutorial

woojoo666
  • 7,801
  • 7
  • 45
  • 57
  • using `Paste the BitBucket repository url (git@bitbucket.org//.git)` will force you to add an ssh key. In case you dont have already an existing one, will be a pain. – Alex Kyriakidis Jun 12 '15 at 03:15
  • 1
    yea BitBucket is mostly for private repos, so you'll need an ssh key to get started. Don't worry, it's not as bad as it looks! I added a quick tutorial to my answer – woojoo666 Jun 12 '15 at 06:28
  • Okay, but next time you should mention a thing like. Also if you delete the repo and re create it, you can use https again. – Alex Kyriakidis Jun 12 '15 at 06:31
  • alright thanks I'll keep that in mind for next time :) – woojoo666 Jun 12 '15 at 06:50
  • IMPORTANT: now BitBucket **required 2048 bit key**, so if you follow this instruction it won't work and give you error _"Key is not long enough. Bitbucket requires RSA keys be at least 2048 bits."_. When you generate key in SourceTree (step 4) in the bottom of the dialogue _change "Number of bits in a generated key" from default 1024 to 2048_ and then it will accept your key – Daniel Nov 16 '17 at 14:58
  • 3
    Why are you guys struggling with SSH and public/private keys when it's much easier to use HTTPS? In the `Clone` dialog, switch the top-right drop down from SSH to HTTPS and get the URL: `https://bitbucket.org/.git` – Dan Dar3 Mar 31 '18 at 06:32
  • 1
    @DanDar3 Yes, thank you, this saved me a lot of hassle. – Excrubulent Mar 15 '19 at 09:18
  • 1
    @DanDar3 wow that is a lot easier, I updated the answer, thanks! – woojoo666 Mar 30 '19 at 00:46
  • This doesn't answer the question. It still requires you to go to the bitbucket url and create a repo before being able to publish. – Maximilian Burszley Apr 08 '19 at 12:47
  • @TheIncorrigible1 you're right, I worded it badly. The question asked if it was possible to publish a repo directly, or did one have to create a repo online first. I edited the beginning of my post to directly answer the question – woojoo666 Apr 08 '19 at 13:02
  • For reference, it's possible to automatically create the remote repo [using the api](https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D) – Maximilian Burszley Apr 08 '19 at 13:37
  • @TheIncorrigible1 actually a user mentioned that in [another answer](https://stackoverflow.com/a/36458615/1852456) but that isn't really answering the question either because OP specifically asked for "using SourceTree" – woojoo666 Apr 08 '19 at 13:47
29

Actually there is a more simple solution (only on Mac version). Just four steps:

  1. Right click on the repository and select "Publish to remote..."

SourceTree Publish to Remote Screeshot

  1. Next window will ask you were to publish (github, bitbucket, etc), and then you are done.
  2. Link the remote repository
  3. Push
David
  • 4,336
  • 2
  • 23
  • 31
  • This must be new. I haven't actually used repositories in a while, so I'll give it a whirl. – cma0014 Nov 12 '15 at 15:06
  • 3
    Actually I've checked that is only on Mac version of SourceTree, not on Windows. :( – David Nov 13 '15 at 22:43
  • Means in github, bitbucket, etc Mac version is simpler than Windows – eQ19 Mar 13 '16 at 13:31
  • This is great, but you "may" have to add a remote like previous answer when you want to push. i.e. 'git remote add origin https://thatsyou@bitbucket.org/some/url.git' from your repo root, you can get the URL from bitbucket. – WickedW Mar 23 '16 at 17:07
  • 1
    Yeah, i have checked that it creates the repo on the remote, but then doesn't link set it on the remotes of the local repository. It would be a nice upgrade for next versions – David Apr 13 '16 at 12:13
  • (On Mac version) this is the best answer. However, this feature seems to be incomplete (or simply broken). It will create the remote reop, but the 'create' window will stay open (as if nothing happened) and can only close by 'Cancel' button. You can now go to the 'remote' tab (same window), see it and copy the URL, since it will not link it either. (You can right-click the remote to get 'Copy clone URL to clipboard'. – bauerMusic May 26 '16 at 18:18
  • 1
    @bauerMusic this happened to me also with the "in progress" window. But not lately. Although as u said...something fails with this feature. It doesn't link to the new remote repo as we all would expect. Despite all, SourceTree is the best GIT manager for Mac that I found. – David May 28 '16 at 10:52
  • 1
    It actually works perfect for me, it creates the repo and link it with the local and remote, maybe since 2016 they improve it – Kross Jul 26 '17 at 14:34
  • Tryed today and yes, improved. – David Dec 09 '17 at 21:48
  • You can always change the remote URL for your clone using the `Settings` icon (top right corner) > `Remotes` > select and edit `origin` or `Repository` menu > `Repository settings` > select and edit `origin`. – Dan Dar3 Mar 31 '18 at 06:35
9

As this video illustrates, creating a repo online first is the usual way to go.

The SourceTree Release Notes do mention for SourceTree 1.5+:

Support creating new repositories under team / organisation accounts in Bitbucket.

So while there is no "publishing" feature, you could create your online repo from SourceTree.

The blog post "SourceTree for Windows 1.2 is here" (Sept 2013) also mention:

Now you can configure your Bitbucket, Stash and GitHub accounts in SourceTree and instantly see all your repositories on those services. Easily clone them, open the project on the web, and even create new repositories on the remote service without ever leaving SourceTree.
You’ll find it in the menu under View > Show Hosted Repositories, or using the new button at the bottom right of the bookmarks panel.

http://blog.sourcetreeapp.com/files/2013/09/hostedrepowindow.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Bitbucket supports a REST API you can use to programmatically create Bitbucket repositories.

Documentation and cURL sample available here: https://confluence.atlassian.com/bitbucket/repository-resource-423626331.html#repositoryResource-POSTanewrepository

$ curl -X POST -v -u username:password -H "Content-Type: application/json" \
   https://api.bitbucket.org/2.0/repositories/teamsinspace/new-repository4 \
   -d '{"scm": "git", "is_private": "true", "fork_policy": "no_public_forks" }'

Under Windows, curl is available from the Git Bash shell.

Using this method you could easily create a script to import many repos from a local git server to Bitbucket.

Joe Savage
  • 873
  • 9
  • 8
2

If you have a local repository in SourceTree, just go to the BitBucket website, create a new repository there, without a .gitignore file, and after creating it, the page will show you the following commands to do what you want to do:

git remote add origin https://[username]@bitbucket.org/[username]/[reponame].git
git push -u origin master

You can use Git Bash in the folder with your local SourceTree repository and paste these two commands. Now when you push a commit from SourceTree, it will be pushed to the online BitBucket repository aswell.

TK-421
  • 294
  • 1
  • 7
  • 26
1

I used this and it worked out well for me. If your directory is

"repo" and your project is "hello" copy the project there

cd /path/to/my/repo

Initialize your directory

git init

Stage the project

git add hello

commit the project

git commit

Add configurations using the email and username you are using in Bitbucket

git config --global user.email
git config --global user.name

Add comment to the project

git commit -m 'comment'

push the project now

git push origin master

Check out of the master

git checkout master
bademba
  • 237
  • 4
  • 14
0

Setup Bitbucket Repository (Command Line with Mac)

Create New APPLICATION from starting with local reposity :

  1. Terminal -> cd ~/Documents (Paste your APPLICATION base directory path)
  2. Terminal -> mkdir (create directory with )
  3. Terminal -> cd (change directory with directory)
  4. BitBucket A/C -> create repository on bitBucket account
  5. Xcode -> create new xcode project with same name
  6. Terminal -> git init (initilize empty repo)
  7. Terminal -> git remote add origin (Ex. https://app@bitbucket.org/app/app.git)
  8. Terminal -> git add .
  9. Terminal -> git status
    1. Terminal -> git commit -m "IntialCommet"
    2. Terminal -> git push origin master

Create APPLICATION clone repository :

  1. Terminal -> mkdir (create directory with )
  2. Terminal -> cd (change directory with directory)
  3. Terminal -> git clone (Ex. https://app@bitbucket.org/app/app.git)
  4. Terminal -> cd
  5. Terminal -> git status (Show edit/updated file status)
  6. Terminal -> git pull origin master
  7. Terminal -> git add .
  8. Terminal -> git push origin master
Shrikant Tanwade
  • 1,391
  • 12
  • 21
0

Another Solution For Windows Users:

This uses Github as a bridge to get to Bitbucket, caused to the lack of publishing directly from the windows Sourcetree app.

  1. Load your local repo into the Github desktop app.
  2. Publish the repo as a private (for privacy - if desired) repo from the Github desktop app into your Github account.
  3. Open your personal / team account in Bitbucket's website
  4. Create a new Bitbucket repo by importing from Github.
  5. Delete the repo in Github.

Once this is done, everything will be loaded into Bitbucket. Your local remotes will probably need to be configured to point to Bitbucket now.

David Ruiz
  • 383
  • 1
  • 4
  • 10
0

(Linux/WSL at least) From the browser at bitbucket.org, create an empty repo with the same name as your local repo, follow the instructions proposed by bitbucket for importing a local repo (two commands to type).

-1

GIT serves it's purpose well for version control and team projects if commits and branches are maintained properly.
Step 1: Clone your local repo using cli as mentioned by above answers

$ cd [path_to_repo]
$ git remote add origin ssh://git@bitbucket.org//.git
$ git push -u origin --all

Step 2: You can follow any of the above steps to push/pull your works. Easy way is to use git gui. It provides Graphical Interface so that it's easy to stage(add)/unstage, commit/uncommit and push/pull. Beginners can easily understand the git process.

$ git gui

(OR)
Step 2: As mentioned above. Cli codes will do the work.

$ git status
$ git add [file_name]
$ git commit _m "[Comit message"]"
$ git push origin master/branch_name

Raj sree
  • 82
  • 6
-1

As an update to Joe's answer, in that you can script the creation of the repository using the API, only the API call I needed was different. This may be because we are using bitbucket server, and not bitbucket cloud.

To create a new repo within a project on our server, I used:

curl -X POST -v -u USER:PASSWORD -H "Content-Type: application/json" \
http://SERVER/rest/api/1.0/projects/PROJECTNAME/repos/ \
-d '{"scmid":"git", "name":"REPONAME"}'

where USER, PASSWORD, SERVER, PROJECTNAME and REPONAME were of course the desired/required values.

The call is documented in the API reference.

/rest/api/1.0/projects/{projectKey}/repos

Create a new repository. Requires an existing project in which this repository will be created. The only parameters which will be used are name and scmId.

The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.

Community
  • 1
  • 1
Baldrickk
  • 4,291
  • 1
  • 15
  • 27