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


当前回答

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

中心创建

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

其他回答

这是我的初始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

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

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

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

ruby开发者:

gem install githubrepo
githubrepo create *reponame*

根据提示输入username和pw

git remote add origin *ctrl v*
git push origin master

来源:Elikem Adadevoh

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

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)

找到了我喜欢的解决方案:https://medium.com/@jakehasler/ howto -create-a-remote-git-repo- fromm-the-command -line-2d6857f49564

你首先需要创建一个Github个人访问令牌

打开你的~/。Bash_profile或~/。Bashrc在您最喜欢的文本编辑器。在文件顶部附近添加以下一行,这里是导出的其他变量所在的位置:

出口GITHUB_API_TOKEN = < your-token-here >

在下面的某个地方,通过你的其他bash函数,你可以粘贴类似于下面的东西:

function new-git() {
    curl -X POST https://api.github.com/user/repos -u <your-username>:$GITHUB_API_TOKEN -d '{"name":"'$1'"}'
}

现在,当你创建一个新项目时,你可以运行命令$ new-git awesome-repo在你的Github用户帐户上创建一个新的公共远程存储库。