我创建了一个新的本地Git存储库:

~$ mkdir projectname
~$ cd projectname
~$ git init
~$ touch file1
~$ git add file1
~$ git commit -m 'first commit'

有没有任何git命令来创建一个新的远程回购,并从这里将我的提交推到GitHub ?我知道打开浏览器去创建一个新的存储库并不是什么大问题,但是如果有一种方法可以从CLI实现这一点,我会很高兴。

我读了大量的文章,但没有一篇提到如何使用git命令从CLI创建远程回购。Tim Lucas的一篇不错的文章“设置一个新的远程git存储库”是我找到的最接近的文章,但是GitHub不提供shell访问。


当前回答

有关创建令牌的说明,请转到这里。这是您将键入的命令(从回答的日期开始)。(替换所有CAPS关键字):

curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations

输入密码后,您将看到下面包含您的令牌。

{
  "app": {
    "name": "YOUR_NOTE (API)",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  },
  "note_url": null,
  "note": "YOUR_NOTE",
  "scopes": [
    "repo"
  ],
  "created_at": "2012-10-04T14:17:20Z",
  "token": "xxxxx",
  "updated_at": "2012-10-04T14:17:20Z",
  "id": xxxxx,
  "url": "https://api.github.com/authorizations/697577"
}

您可以随时到这里撤销您的令牌

其他回答

如何使用Bash Shell快速创建远程存储库

每次创建存储库时输入完整的代码是很麻烦的

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}' git远程添加源git@github.com:USER/REPO.git Git push origin master

一个更简单的方法是:

在/home/USER_NAME/Desktop/my_scripts目录下创建一个shell脚本githubscript.sh 修改并保存以下代码到githubscript.sh文件中

#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/$1.git;

注意:这里$1是在调用脚本时作为参数传递的存储库名称 在保存脚本之前更改YOUR_GITHUB_USER_NAME。

为脚本文件设置所需的权限 Chmod 755 githubscript.sh 在环境配置文件中包含脚本目录。 纳米~ / . profile; 导出路径= " $路径:$ HOME /桌面/ my_scripts” 还要设置一个别名来运行githubscript.sh文件。 纳米~ / . bashrc; 别名githubrepo="bash githubscript.sh" 现在在终端中重新加载.bashrc和.profile文件。 ~ /来源。bashrc ~ / . profile (; 现在创建一个新的存储库,即demo: githubrepo演示;

这可以用三个命令来完成:

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
git remote add origin git@github.com:nyeates/projectname.git
git push origin master

(v3 Github API更新)

这些命令的解释…

创建github回购

    curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'

curl is a unix command (above works on mac too) that retrieves and interacts with URLs. It is commonly already installed. "-u" is a curl parameter that specifies the user name and password to use for server authentication. If you just give the user name (as shown in example above) curl will prompt for a password. If you do not want to have to type in the password, see githubs api documentation on Authentication "-d" is a curl parameter that allows you to send POST data with the request You are sending POST data in githubs defined API format "name" is the only POST data required; I like to also include "description" I found that it was good to quote all POST data with single quotes ' '

定义推到哪里

git remote add origin git@github.com:nyeates/projectname.git

在github上添加连接(远程)回购的位置和存在的定义 “origin”是git用来表示源代码来源的默认名称 技术上没有来自github,但现在github回购将记录的来源 “git@github.com:nyeates”是一个SSH连接,它假设你已经在github上设置了一个可信的SSH密钥对。

将本地回购推到github

git push origin master

从主本地分支推送到原始远程(github)

由于代表的原因,我不能将其作为注释添加(它最好与bennedich的答案一起使用),但对于Windows命令行,以下是正确的语法:

curl -u YOUR_USERNAME https://api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"

这是相同的基本形式,但必须使用双引号(")而不是单引号,并使用反斜杠转义POST参数中发送的双引号(在-d标志之后)。我还删除了我的用户名周围的单引号,但如果你的用户名有空格(可能吗?),它可能需要双引号。

简单步骤(使用git + hub => GitHub):

Install Hub (GitHub). OS X: brew install hub having Go: go get github.com/github/hub otherwise (having Go as well): git clone https://github.com/github/hub.git && cd hub && ./script/build Go to your repo or create empty one: mkdir foo && cd foo && git init. Run: hub create, it'll ask you about GitHub credentials for the first time. Usage: hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME] Example: hub create -d Description -h example.com org_name/foo_repo Hub will prompt for GitHub username & password the first time it needs to access the API and exchange it for an OAuth token, which it saves in ~/.config/hub. To explicitly name the new repository, pass in NAME, optionally in ORGANIZATION/NAME form to create under an organization you're a member of. With -p, create a private repository, and with -d and -h set the repository's description and homepage URL, respectively. To avoid being prompted, use GITHUB_USER and GITHUB_PASSWORD environment variables. Then commit and push as usual or check hub commit/hub push.

有关更多帮助,请运行:hub help。

参见:在GitHub中使用命令行导入Git存储库。

如果您安装了defunkt的优秀Hub工具,那么这将变得非常简单

中心创建

用作者的话来说,“hub是git的命令行包装器,可以让你更好地使用GitHub。”