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


当前回答

对于使用双因素身份验证的用户,您可以使用bennedich的解决方案,但您只需要为第一个命令添加X-Github-OTP报头。将CODE替换为从双因素身份验证提供程序获得的代码。将USER和REPO替换为存储库的用户名和名称,就像在他的解决方案中那样。

curl -u 'USER' -H "X-GitHub-OTP: CODE" -d '{"name":"REPO"}' https://api.github.com/user/repos
git remote add origin git@github.com:USER/REPO.git
git push origin master

其他回答

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

目前公认的答案和投票最多的答案都已经过时了。密码身份验证已弃用,并将于2020年11月13日16:00 UTC移除。

现在使用GitHub API的方式是通过个人访问令牌。

你需要(替换ALL CAPS关键字):

通过网站创建个人访问令牌。是的,您必须使用浏览器,但以后所有访问都只能使用一次。安全地存储令牌。 创建回购通道

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos  -d '{"name":"REPO"}'

或者,从一开始就设置为私有:

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'

添加新的原点并将其推入:

git remote add origin git@github.com:USER/REPO.git
git push origin master

这样做的缺点是每次都必须输入令牌,并且它会出现在bash历史记录中。

为了避免这种情况,你可以

将头文件存储在一个文件中(我们称它为HEADER_FILE)

Authorization: token MY_ACCESS_TOKEN

curl从文件中读取了吗

curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo

为了更加安全,您可以将访问权限设置为400,并将用户设置为root

chmod 400 HEADER_FILE
sudo chown root:root HEADER_FILE

现在需要sudo来访问头文件

sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo

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

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)

ruby开发者:

gem install githubrepo
githubrepo create *reponame*

根据提示输入username和pw

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

来源:Elikem Adadevoh

基于@机械蜗牛的另一个回答,除了没有使用python,我发现这太过分了。添加到~/.gitconfig:

[github]
    user = "your-name-here"
[alias]
    hub-new-repo = "!REPO=$(basename $PWD) GHUSER=$(git config --get github.user); curl -u $GHUSER https://api.github.com/user/repos -d {\\\"name\\\":\\\"$REPO\\\"} --fail; git remote add origin git@github.com:$GHUSER/$REPO.git; git push origin master"