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


当前回答

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

其他回答

有一个官方的github宝石,我认为,这样做。随着我的学习,我会尝试添加更多的信息,但我现在才刚刚发现这个宝石,所以我知道的还不多。

更新:设置我的API密钥后,我能够通过创建命令在github上创建一个新的repo,但是我不能使用create-from-local命令,这应该是采取当前的本地repo,并在github上做出相应的远程。

$ gh create-from-local
=> error creating repository

如果有人对此有一些见解,我很想知道我做错了什么。已经有问题了。

更新:我最终让这个工作。我不确定如何重新产生的问题,但我只是从头开始(删除。git文件夹)

git init
git add .emacs
git commit -a -m "adding emacs"

现在这一行将创建远程回购,甚至推到它,但不幸的是,我认为我不能指定我想要的回购的名称。我想要它被称为“dotfiles”在github上,但gh宝石只是使用当前文件夹的名称,这是“jason”,因为我是在我的家庭文件夹。(我添加了一张要求所需行为的票)

gh create-from-local

另一方面,这个命令接受一个参数来指定远程repo的名称,但它用于从头开始一个新项目,即在调用这个命令后,您将获得一个新的远程repo,它在相对于当前位置的新创建的子文件夹中跟踪一个本地repo,两者的名称都指定为参数。

gh create dotfiles

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

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

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)

对于所有的Python 2.7。*用户。目前版本3的Github API有一个名为GitPython的Python包装器。只需使用easy_install PyGithub或pip install PyGithub安装即可。

from github import Github
g = Github(your-email-addr, your-passwd)
repo = g.get_user().user.create_repo("your-new-repos-name")

# Make use of Repository object (repo)

Repository对象文档在这里。

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

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