从 Git Bash 终端在 Bitbucket 上创建新的存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13788485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create new repo on Bitbucket from Git Bash terminal?
提问by Patrick
Is it possible to create a new repository in Bitbucket by using command line Git? I have tried the following:
是否可以使用命令行 Git 在 Bitbucket 中创建新存储库?我尝试了以下方法:
git clone --bare https://[email protected]/username/new_project.git
I get this message:
我收到这条消息:
Cloning into bare repository 'new_project.git'...
fatal:https://[email protected]/username/new_project.git/info/refs
not found: did you run git update-server-info on the server?
克隆到裸存储库“new_project.git”...
致命:https://[email protected]/username/new_project.git/info/refs
未找到:您是否在服务器上运行了 git update-server-info?
It would be nice to do this without going to the web app.
如果不访问 Web 应用程序就可以执行此操作会很好。
回答by Marek
You can use the Bitbucket REST API and cURL. For example:
您可以使用 Bitbucket REST API 和 cURL。例如:
curl --user login:pass https://api.bitbucket.org/1.0/repositories/ \
--data name=REPO_NAME
to create new repository named REPO_NAME
.
创建名为REPO_NAME
.
See Use the Bitbucket REST APIsfor more information.
有关更多信息,请参阅使用 Bitbucket REST API。
UPDATE
更新
For Bitbucket V2 specifically, see POST a new repo
对于 Bitbucket V2,请参阅POST a new repo
回答by mcfw
https://confluence.atlassian.com/bitbucket/repository-resource-423626331.html
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" }'
回答by pztrick
Here is @hannesr's scripttweaked a bit to accept input from prompts:
这是@hannesr 的脚本稍微调整了一下以接受来自提示的输入:
# 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 [email protected]:$username/$reponame.git
git push -u origin --all
git push -u origin --tags
}
You should place this in your .bashrc
or .bash_aliases
.
你应该把它放在你的.bashrc
或.bash_aliases
.
回答by gak
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.
cURL 的最佳答案对我来说效果不佳,所以我最终用Bitbucket-API在 Python 中完成了它。这是有关repository.create()调用的文档。
Install:
安装:
pip install bitbucket-api
Python:
Python:
>>> from bitbucket.bitbucket import Bitbucket
>>> bb = Bitbucket(username, password)
>>> bb.repository.create('awesome-repo', scm='git', private=True)
(True, {u'scm': ...})
回答by hannesr
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.
我制作了一个快速的 shell 脚本,它负责在当前工作目录中创建一个本地 git,执行“初始提交”,然后创建 bitbucket 存储库(使用 Mareks curl 方法),然后最后完成推送初始文件所需的所有操作致力于bitbucket。
(note this is for private repos only but that is easily changed as described by Patrick)
(请注意,这仅适用于私人存储库,但很容易按照 Patrick 的描述进行更改)
Use it like this:
像这样使用它:
fillbucket <user> <password> <reponame>
Code is on http://bitbucket.org/hannesr/fillbucket
回答by OpSocket
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/ -d '{\"is_private\": \"true\", \"scm\": \"git\", \"project\": \"KEY_OF_PROJECT\"}' | jq '.links.clone[].href'; }; f"
NOTE :You should updateconstants with your informations.
注意:您应该使用您的信息更新常量。
This way your password is not stored into your .bash_history.
这样您的密码就不会存储到您的.bash_history 中。
It has to be a one-liner to fit inside your ~/.gitconfig
file.
它必须是单衬才能放入您的~/.gitconfig
文件中。
Usage
用法
git create <repository_name>
This either return nullor your newly created repository addresses.
这要么返回null要么返回您新创建的存储库地址。
You can remove the jq part if you can't / don't want to install it.
如果您不能/不想安装它,您可以删除 jq 部分。
Sweetness
甜度
Cheers!
干杯!
EDIT :I had to replace
Content-Type: application/json
withContent-Type: application/x-www-form-urlencoded
because somehow the -dflag 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.
编辑:我不得不替换
Content-Type: application/json
为Content-Type: application/x-www-form-urlencoded
因为不知何故-d标志现在将标头设置为后者,即使您指定要发送 json。cURL 手册说:
(HTTP) 将 POST 请求中的指定数据发送到 HTTP 服务器,就像浏览器在用户填写 HTML 表单并按下提交按钮时所做的那样。这将导致 curl 使用内容类型 application/x-www-form-urlencoded 将数据传递到服务器。与 -F, --form 比较。
回答by user1159415
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:
我对脚本上面的@pztrick做了一点修改。这个新脚本应该可以正常工作,但它使用较新的 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 [email protected]:$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).
您可以将其放在 .bashrc 或 .bash_aliases 文件中(就像原始脚本一样)。
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.
请注意,它还会将其创建为私有存储库。您可以将 "is_private": "true" 更改为 "is_private": "false" 以使其成为公共存储库。
回答by Oscar Morrison
@hannester I forked and slightly modified your script.
@hannester 我分叉并稍微修改了你的脚本。
You had the incorrect remote url (you left your username in the script). Modified it to included Username and Password in the script file.
您的远程 URL 不正确(您在脚本中留下了您的用户名)。将其修改为在脚本文件中包含用户名和密码。
And renamed, with instructions on how to add to path:
并重命名,并附有关于如何添加到路径的说明: