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


当前回答

有一个官方的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

其他回答

基于@机械蜗牛的另一个回答,除了没有使用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"

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

中心创建

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

最后,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

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

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