我创建了一个新的本地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访问。


当前回答

在命令行上创建一个新的存储库

echo "# <RepositoryName>" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

从命令行推送现有存储库

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

其他回答

这是我的初始git命令(可能,这个操作发生在C:/Documents and Settings/your_username/):

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

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

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)

简单步骤(使用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存储库。

基于Bennedich的回答,我创建了一个Git别名。在~/.gitconfig中添加以下命令:

[github]
    user = "your_github_username"
[alias]
    ; Creates a new Github repo under the account specified by github.user.
    ; The remote repo name is taken from the local repo's directory name.
    ; Note: Referring to the current directory works because Git executes "!" shell commands in the repo root directory.
    hub-new-repo = "!python3 -c 'from subprocess import *; import os; from os.path import *; user = check_output([\"git\", \"config\", \"--get\", \"github.user\"]).decode(\"utf8\").strip(); repo = splitext(basename(os.getcwd()))[0]; check_call([\"curl\", \"-u\", user, \"https://api.github.com/user/repos\", \"-d\", \"{{\\\"name\\\": \\\"{0}\\\"}}\".format(repo), \"--fail\"]); check_call([\"git\", \"remote\", \"add\", \"origin\", \"git@github.com:{0}/{1}.git\".format(user, repo)]); check_call([\"git\", \"push\", \"origin\", \"master\"])'"

要使用它,运行

$ git hub-new-repo

从本地存储库中的任何地方,并在提示时输入您的Github密码。

你可以使用GitHub API通过命令行创建一个GitHub repo。检查存储库API。如果你向下滚动大约三分之一的方式,你会看到一个名为“创建”的部分,解释如何通过API创建一个回购(正上方是一个部分,解释如何用API派生一个回购)。显然你不能使用git来做这件事,但是你可以使用curl这样的工具通过命令行来做这件事。

在API之外,没有办法通过命令行在GitHub上创建一个repo。正如你所注意到的,GitHub不允许shell访问等,所以除了GitHub API,创建回购的唯一方法是通过GitHub的web界面。