Alfred Sun +

利用 CLI 新建 GitHub 仓库

Posted by Alfred Sun git, github

GitHub does not provide shell access !

用过 GitHub 的都知道,一般通过它的网页接口来创建新的仓库 Repository。
由于 GitHub 不提供 Shell Access 的权限,所以无法通过普通的终端命令来新建 Repository。

那么,有没有什么办法能利用 CLI 来新建 Repository 呢?

其实,我们可以利用 GitHub 提供 Repository API 来做到。API 告诉我们可以通过发送 HTTP 请求来新建仓库,显然 git 命令无法使用,但是可以利用 curl 这个工具做到。

GitHub API 新建远程 Repository

GitHub Create Repository API 规范:

POST /user/repos

# Input data example:
  "name": "Hello-World",
  "description": "This is your first repository",
  "homepage": "",
  "private": false,
  "has_issues": true,
  "has_wiki": true,
  "has_downloads": true

1、CLI 新建 Repository

# GitHub Account: alfred
# New repository name: test

curl -u 'alfred' -d '{"name":"test", "description":"This project is a test"}'

# "-u":     specifies the user name and password to use for server authentication
# "-d":     allows you to send POST data with the request
# "name":   the only POST data required; I like to also include "description"

# git remote add origin
# git remote add origin
#    add definition for location and existance of connected (remote) repo on github
#    "origin" is a default name used by git for where the source came from; technically didn't come from github, but now the github repo will be the source of record
#    "" is a ssh connection that assumes you have already setup a trusted ssh keypair with github.
# git push origin master

2、初始化 Repository

# Creates a directory for your project called "test" in your user directory
mkdir ~/test
# Changes the current working directory to your newly created directory
cd ~/test
# create a file to commit, named
# Sets up the necessary Git files
git init
# Stages your file, adding it to the list of files to be committed
git add
# Commits your files, adding the message
git commit -m 'first initial commit for master branch'
# Creates a remote named "origin" pointing at your GitHub repository
git remote add origin
# Sends your commits in the "master" branch to GitHub
git push -u origin master

方便起见,写了个 Bash 脚本 专门做上面的那些事情,用法:
bash repo-name


[A]: 在执行 $ git remote add origin

错误提示:fatal: remote origin already exists.


# 先删除存在的旧的 remote
$ git remote rm origin
# 然后添加新的 remote
$ git remote add origin

# 或者直接重设 remote 的 URL
$ git remote set-url origin

[B]: 在执行 $ git push origin master

错误提示:error:failed to push som refs to......


# 先把远程服务器github上面的文件拉下来,再push 上去
$ git pull origin master


删除 GitHub Repository

同样,利用 API 并通过 curl 命令发送 “DELETE” 请求来实现删除 GitHub 仓库。

GitHub Delete Repository API 规范:

DELETE /repos/:owner/:repo

在系统终端输入下面命令,向指定的 URL 发送删除请求:

# curl -X "DELETE"

curl -u 'alfred' -X 'DELETE'

GitHub SSH 帮助

我们可以使用 GitHub for Windows 来向 GitHub 传输数据,可以从其他仓库(Subversion、Mercurial 或者 TFS project)导入数据初始化 GitHub 仓库,还可以像上面一样使用 Git 的协议

GitHub Repository Setup

1、GitHub 两种协议

GitHub 支持两种协议来传输数据:HTTPS 协议和 SSH 协议。

如果本地仓库使用 HTTPS 协议,可以保存 GitHub 密码,避免每次 push 都要认证,看这篇文章的说明;而如果使用 SSH 协议传输数据,那么需要在本地生成新的 SSH key,并与 GitHub 账户关联起来。操作方法参见 GitHub 帮助文档 Generating SSH keys 或者博主写的另一篇文章内的中文说明

2、SSH 访问自动认证授权

使用 SSH 也有个问题,就是每次 pull 或 push 时,都要求输入 SSH key 的密码。我们可以通过 ssh-agent 记住密码自动认证:

$ ssh-agent bash
$ ssh-add 
Enter passphrase for /home/alfred/.ssh/id_rsa: 
Identity added: /home/alfred/.ssh/id_rsa (/home/alfred/.ssh/id_rsa)


# start the ssh-agent in the background
ssh-agent -s
# Agent pid 59566
ssh-add ~/.ssh/id_rsa

如果报出这个错误:Could not open a connection to your authentication agent,那么:

# start the agent for MsysGit or Cygwin Bash
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa

# test if the identity key is added
ssh-add -l


SSH private-keys are usually stored encrypted on the computers they are stored on. A pass-phrase is used to decrypt them when they are to be used. Since most people use SSH public-private key-pairs to get around typing in passwords all the time, the ssh-agent daemon exists to store decrypted private-keys you plan on using in a given session. The thing most people get tripped up on when using ssh-agent is that what the program outputs, some borne or csh shell commands, needs to be run. It may look like ssh-agent has set some variables for you, but it has in fact done no such thing. If you call ssh-add without processing ssh-agent’s output, it will complain it is unable to open a connection to your authentication agent. The most straightforward way to run ssh-agent on the command line is as follows: eval `ssh-agent`. After doing this, calls to ssh-add should succeed without error.

ssh-addssh 与 ssh agent 通信依赖一个环境变量,如果启动多个命令窗口,或者错误启动,那么 ssh-addssh 都不能读取环境变量(设置在命令提示符本地)。

如果正在使用 Cygwin,那么参考 SSH Agent on Cygwin

# Add to your Bash config file
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
    trap "kill $SSH_AGENT_PID" 0

这样对于每个命令提示符窗口,都会自动启动 ssh-agent。



Related Blog