Create new repo on Bitbucket from Git Bash terminal?

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

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 

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



回答6:

@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



回答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:

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/json' 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" 

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 

This either return null or your newly created repository addresses.

You can remove the jq part if you don't want install it with brew install jq

Sweetness

Cheers!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!