Create new repo on Bitbucket from Git Bash terminal?

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.

54110 次浏览

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

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

@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

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.

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': ...})

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" }'

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.

  • 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.