78

Is it possible to create a new repository in Bitbucket by using command line Git? I have tried the following:

git clone --bare https://username@bitbucket.org/username/new_project.git

I get this message:

Cloning into bare repository 'new_project.git'...
fatal: https://username@bitbucket.org/username/new_project.git/info/refs not found: did you run git update-server-info on the server?

It would be nice to do this without going to the web app.

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
Patrick Jackson
  • 18,766
  • 22
  • 81
  • 141

8 Answers8

95

You can use the Bitbucket REST API and cURL. For example:

curl --user login:pass https://api.bitbucket.org/1.0/repositories/ \
--data name=REPO_NAME

to create new repository named REPO_NAME.

See Use the Bitbucket REST APIs for more information.

UPDATE

For Bitbucket V2 specifically, see POST a new repo

dirkdirk
  • 171
  • 2
  • 10
Marek
  • 1,878
  • 14
  • 20
  • 36
    Thanks, simple easy solution. Worth noting above creates a public repo. Just add --data is_private='true' for a private repo – Patrick Jackson Dec 09 '12 at 15:40
  • 4
    For me I was entering the wrong password but curl didn't tell me it got a 401 Unauthorized. Adding -v let me see what was going on. – funroll Sep 20 '13 at 18:20
  • Also if you look in bitbucket, the git url they show you doesn't use camelcase for the repositoryname.git. But you can use camelcase anyway and it works fine. – funroll Sep 20 '13 at 19:00
  • 3
    I needed to create a repo not under my account, but under a team I am a member of. The API shows this, but add --data owner= – Chad Gorshing Sep 23 '13 at 22:57
  • 11
    From a security point of view, I would change `--user login:pass` to just `--user login` and enter password manually each time, so it is not saved in your `.bash_history` (or, if you are on OS X you could securely retrieve password from keychain using command line, more info here for example: http://joshtronic.com/2014/02/17/using-keyring-access-on-the-osx-commandline/). – ivanzoid Feb 13 '15 at 15:18
  • 2
    @Stephen 's post below http://stackoverflow.com/a/19670989/951349 is a bit more current. – SmileBot Mar 14 '15 at 02:28
  • Here's a link to more comprehensive documentation: https://confluence.atlassian.com/display/BITBUCKET/repository+Resource – aaronbauman Apr 29 '15 at 15:08
  • It'd be nice if you didn't escape the newline and left `--data name=NAME` with the `curl` command. Took me a few looks to figure out why when it could've been unambiguous – Maximilian Burszley Oct 17 '17 at 19:57
  • 1
    This api is not supported anymore I get this error : This API is no longer supported.\n\nFor information about its removal, please refer to the deprecation notice at – Tarun Gupta Jan 31 '20 at 13:50
15

https://confluence.atlassian.com/bitbucket/repository-resource-423626331.html

$ 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" }'
mcfw
  • 151
  • 1
  • 2
13

Here is @hannesr's script tweaked a bit to accept input from prompts:

# startbitbucket - creates remote bitbucket repo and adds it as git remote to cwd
function startbitbucket {
    echo 'Username?'
    read username
    echo 'Password?'
    read -s password  # -s flag hides password text
    echo 'Repo name?'
    read reponame

    curl --user $username:$password \
         https://api.bitbucket.org/1.0/repositories/ \
         --data name=$reponame \
         --data is_private='true'
    git remote add origin git@bitbucket.org:$username/$reponame.git
    git push -u origin --all
    git push -u origin --tags
}

You should place this in your .bashrc or .bash_aliases.

Michael
  • 8,362
  • 6
  • 61
  • 88
pztrick
  • 3,741
  • 30
  • 35
  • 4
    I like the simplicity of this solution, but I would use "read -s" for the password field. This simply turns off the echo so that when the password is entered it is not readable at the prompt (or in the scrollback buffer) – Ch4ni Aug 26 '15 at 01:22
  • 1
    @pztrick: You can combine `echo` and `read` into a single line, e.g. `read -p 'Username?' username` – ccpizza Dec 24 '15 at 12:22
  • `bitbucket-cli` (`sudo pip2 install bitbucket-cli`) does this in a more secure way `bitbucket create --private --protocol ssh --scm git YOUR_REPO_NAME` – Jonathan Dec 20 '17 at 22:41
4

I've made a slight modification to @pztrick above script. This new script should work the same, but it uses the newer 2.0 API:

function startbitbucket {
    echo 'Username?'
    read username
    echo 'Password?'
    read -s password  # -s flag hides password text
    echo 'Repo name?'
    read reponame

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

    git remote add origin git@bitbucket.org:$username/$reponame.git
    git push -u origin --all
    git push -u origin --tags
}

You can place this in your .bashrc or .bash_aliases file (just like the original script).

Note that it will also create this as a private repo. You can change "is_private": "true" to "is_private": "false" to make it a public repo.

user1159415
  • 111
  • 7
2

I made a quick shell script that takes care of creating a local git in current working directory, doing the "Initial commit" and then create the bitbucket repo (using Mareks curl method), and then finally doing all that is needed to push the initial commit to bitbucket.

(note this is for private repos only but that is easily changed as described by Patrick)

Use it like this:

fillbucket <user> <password> <reponame>

Code is on http://bitbucket.org/hannesr/fillbucket

hannesr
  • 449
  • 5
  • 12
2

The top answer with cURL wasn't working well for me, so I ended up doing it in Python with Bitbucket-API. Here's the documentation on the repository.create() call.

Install:

pip install bitbucket-api

Python:

>>> from bitbucket.bitbucket import Bitbucket
>>> bb = Bitbucket(username, password)
>>> bb.repository.create('awesome-repo', scm='git', private=True)
(True, {u'scm': ...})
gak
  • 32,061
  • 28
  • 119
  • 154
2
  • If you are using Bitbucket Cloud(bitbucket.org) You can use the Bitbucket REST API and cURL and also to specify the Project key for the repository you can use this:-

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

This will create a repository under a specific Project.

  • If you want to create a repository in Bitbucket server V1.0 REST API is available for that. The command would look like this:-

curl -u $username:$password -X POST -H "Content-Type:application/json" -d '{"name": "'$reponame'","scmId": "git","forkable": true}' http://localhost:7990/rest/api/1.0/projects/${project_key,,}/repos

You can mention your company url(exmple - birbucket.xyz.com) in place of localhost:7990.

See Use the Bitbucket Server REST APIs for more information on bitbucket server.

0

@hannester I forked and slightly modified your script.

You had the incorrect remote url (you left your username in the script). Modified it to included Username and Password in the script file.

And renamed, with instructions on how to add to path here:

https://bitbucket.org/oscarmorrison/newgit

  • 3
    Hi Oscar, and welcome to StackOverflow. Thank you for your contribution! I have one small suggestion though; it would be better to include the script in your answer (not only link to it) because that way the answer could never be lost and will always be paired with the question. – ljgw Aug 12 '14 at 11:18