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


当前回答

ruby开发者:

gem install githubrepo
githubrepo create *reponame*

根据提示输入username和pw

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

来源:Elikem Adadevoh

其他回答

最后,GitHub正式宣布了他们所有核心功能的新CLI。

点击这里查看:https://cli.github.com/

通过HomeBrew安装:brew install gh用于其他方式:https://github.com/cli/cli#installation

then

gh repo create

其他可用的特性。

$ gh --help

Work seamlessly with GitHub from the command line.

USAGE
  gh <command> <subcommand> [flags]

CORE COMMANDS
  gist:       Create gists
  issue:      Manage issues
  pr:         Manage pull requests
  release:    Manage GitHub releases
  repo:       Create, clone, fork, and view repositories

ADDITIONAL COMMANDS
  alias:      Create command shortcuts
  api:        Make an authenticated GitHub API request
  auth:       Login, logout, and refresh your authentication
  completion: Generate shell completion scripts
  config:     Manage configuration for gh
  help:       Help about any command

FLAGS
  --help      Show help for command
  --version   Show gh version

EXAMPLES
  $ gh issue create
  $ gh repo clone cli/cli
  $ gh pr checkout 321

ENVIRONMENT VARIABLES
  See 'gh help environment' for the list of supported environment variables.

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

FEEDBACK
  Open an issue using 'gh issue create -R cli/cli'

现在你可以在终端上创建repo了。

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

目前公认的答案和投票最多的答案都已经过时了。密码身份验证已弃用,并将于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

有关创建令牌的说明,请转到这里。这是您将键入的命令(从回答的日期开始)。(替换所有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"
}

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

github API v3的CLI命令(替换所有CAPS关键字):

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin git@github.com:USER/REPO.git
git push origin master