三种直接在命令行创建Github仓库的方法
摘要: 不需要在github网页上创建仓库,直接用命令行搞定,此文介绍三种直接在命令行创建GitHub仓库的形式!
本文最后更新于 2018-12-08【2264 天前】,文中所描述的信息可能已发生改变,请谨慎使用。如有问题或建议,欢迎在文章底部留言参与讨论!
摘要: 不需要在github网页上创建仓库,直接用命令行搞定,此文介绍三种直接在命令行创建GitHub仓库的形式!
准备工作
进入一个本地仓库,并初始化
git init && git add . && git commit -m "Init"
- 新建一个 API Token
进入github - settings - Personal access tokens
, generate new token, 写入 description,选择 scopes (权限范围)。记住personal access token
(那串数字,只显示一遍!),请记住它,下次就看不到了!
一、命令行格式
这是最直接的一种形式,直接把参数写到命令行搞定:
curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'
需要把$username
和$token
分别换成实际的用户名和刚刚得到的personal access token
,把$repo_name
换成任何想要的repo name
.
二、bash形式
可以把命令行写成 bash 脚本,下次只要执行里面的简单命令就可以执行以上整条命令。
把
username
和token
写入~/.gitconfig
, 形式如下:[github] user = your user name token = the token you get
把如下的 bash code 写入
~/.bashrc
或者~/.bash_profile
文件:github-create() { repo_name=$1 dir_name=`basename $(pwd)` if [ "$repo_name" = "" ]; then echo "Repo name (hit enter to use '$dir_name')?" read repo_name fi if [ "$repo_name" = "" ]; then repo_name=$dir_name fi username=`git config github.user` if [ "$username" = "" ]; then echo "Could not find username, run 'git config --global github.user <username>'" invalid_credentials=1 fi token=`git config github.token` if [ "$token" = "" ]; then echo "Could not find token, run 'git config --global github.token <token>'" invalid_credentials=1 fi if [ "$invalid_credentials" == "1" ]; then return 1 fi echo -n "Creating Github repository '$repo_name' ..." curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' > /dev/null 2>&1 echo " done." echo -n "Pushing local code to remote ..." git remote add origin git@github.com:$username/$repo_name.git > /dev/null 2>&1 git push -u origin master > /dev/null 2>&1 echo " done." }
- 使上述命令生效:重新打开或新启动一个 Terminal,或者运行
source ~/.bashrc
以后便可以使用如下命令创建远程仓库了:
github-create [repo_name]
其中,默认的repo名是当前目录名。
三、Bash形式简化版
把如下code写入
~/.bashrc
:github-create() {if [ $1 ] then repo_name=$1 else echo "Repo name?" read repo_name fi curl -u '$username:$token' https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' git remote add origin git@github.com:efatsi/$repo_name.git git push -u origin master }
- 同 Bash 方式的步骤3
执行命令
simple-create [repo_name]
转自开源中国,有修改。
文章目录
本文作者:Quanyin Tang
本文链接:三种直接在命令行创建Github仓库的方法 - https://www.imtqy.com/create-github-repo-with-cli.html
版权声明:如无特别声明,本文即为原创文章,仅代表个人观点,版权归 Quanyin 所有,未经允许禁止转载,经授权转载请注明出处!
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。