Create new repo on Bitbucket from Git Bash termina

2019-01-29 23:53发布

问题:

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.

回答1:

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



回答2:

More recently, we can just use bitbucket-cli.

Install it using pip

pip install bitbucket-cli

Then create a repo using

bitbucket create --private --protocol ssh --scm git YOUR_REPO_NAME

Note that this creates a private git repo, you can use --public for public access and --scm hg if you use Mercurial. Username argument can be added via --username YOUR_USER_NAME.



回答3:

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.



回答4:

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


回答5:

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



回答6:

Here's the one-liner copy/paste candy:

# This is Git's per-user configuration file.
[alias]
  create = "!f() { curl -X POST -u YOUR_EMAIL_ADDRESS -H 'Content-Type: application/x-www-form-urlencoded' https://api.bitbucket.org/2.0/repositories/YOUR_USERNAME_OR_TEAM_NAME/$1 -d '{\"is_private\": \"true\", \"scm\": \"git\", \"project\": \"KEY_OF_PROJECT\"}' | jq '.links.clone[].href'; }; f"

NOTE : You should update constants with your informations.

This way your password is not stored into your .bash_history.

It has to be a one-liner to fit inside your ~/.gitconfig file.

Usage

git create <repository_name>

This either return null or your newly created repository addresses.

You can remove the jq part if you can't / don't want to install it.

Sweetness

Cheers!

EDIT : I had to replace Content-Type: application/json with Content-Type: application/x-www-form-urlencoded because somehow the -d flag now sets the header to the latter even if you specify you are sending json.

cURL manual says:

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.



回答7:

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


回答8:

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.



回答9:

@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:

https://bitbucket.org/oscarmorrison/newgit



标签: git bitbucket