首页 文章

是否可以在不打开浏览器的情况下从CLI在GitHub上创建远程仓库?

提问于
浏览
313

我创建了一个新的本地Git存储库:

~$ mkdir projectname
~$ cd projectname
~$ git init
~$ touch file1
~$ git add file1
~$ git commit -m 'first commit'

Is there any git command to create a new remote repo and push my commit to GitHub from here? 我知道启动浏览器并转到Create a New Repository没什么大不了的,但如果有办法从CLI实现这一点,我会很高兴 .

我阅读了大量的文章,但我没有提到如何使用git命令从CLI创建远程仓库 . Tim Lucas的好文章Setting up a new remote git repository是我找到的最接近的文章,但GitHub不提供shell访问权限 .

22 回答

  • 3

    您可以使用GitHub API通过命令行创建GitHub存储库 . 看看repository API . 如果向下滚动大约三分之一,你会看到一个 Headers 为"Create"的部分,它解释了如何通过API创建一个repo(上面是一个解释如何用API分解repo的部分) . 显然你不能使用 git 来做这件事,但你可以通过命令行使用像 curl 这样的工具来完成 .

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

  • 2

    用于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
    
  • 2

    对于具有双因素身份验证的用户,您可以使用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
    
  • 49

    这可以通过三个命令完成:

    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 repo

    curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
    
    • curl是一个unix命令(上面也适用于mac),它检索URL并与之交互 . 它通常已经安装 .

    • "-u"是一个curl参数,它指定用于服务器身份验证的用户名和密码 .

    • 如果您只是提供用户名(如上例所示),curl将提示输入密码 .

    • 如果您不想输入密码,请参阅Authentication上的githubs api文档

    • "-d"是一个curl参数,允许您使用请求发送POST数据

    • 您正在githubs defined API format发送POST数据

    • "name"是唯一需要的POST数据;我也想包括"description"

    • 我发现用单引号引用所有POST数据是好的' '

    定义要推送的位置

    git remote add origin git@github.com:nyeates/projectname.git
    
    • 在github上添加连接(远程)repo的位置和存在的定义

    • "origin"是git用于源来源的默认名称

    • 技术上没有来自github,但现在github repo将成为记录的来源

    • "git@github.com:nyeates"是一个ssh连接,假设您已经使用github设置了受信任的ssh密钥对 .

    将本地仓库推送到github

    git push origin master
    
    • 从主本地分支推送到源远程(github)
  • 0

    如果你安装defunkt's优秀的Hub工具,那就变得如此简单

    git create

    用作者的话说,“hub是git的命令行包装器,可以让你在GitHub上更好 . ”

  • -5

    简单步骤(使用 git hub => GitHub):

    • 安装HubGitHub) .

    • OS X: brew install hub

    • Gogo get github.com/github/hub
      否则

    • (也有Go):

    git clone https://github.com/github/hub.git && cd hub && ./script/build
    
    • 转到您的仓库或创建空仓: mkdir foo && cd foo && git init .

    • 运行: hub create ,它会首次询问您有关GitHub凭据的信息 .

    用法: hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]

    示例: hub create -d Description -h example.com org_name/foo_repo

    Hub将在第一次访问API时提示输入GitHub用户名和密码,并将其交换为OAuth令牌,并将其保存在〜/ .config / hub中 . 要明确命名新存储库,请传入NAME,可选地以ORGANIZATION / NAME形式传入,以在您所属的组织下创建 . 使用-p,创建一个私有存储库,并使用-d和-h分别设置存储库的描述和主页URL . 要避免被提示,请使用GITHUB_USER和GITHUB_PASSWORD环境变量 .

    • 然后像往常一样提交并推送或检查 hub commit / hub push .

    如需更多帮助,请运行: hub help .

    另见:Importing a Git repository using the command line at GitHub .

  • 3

    有一个official github gem,我认为,这样做 . 我刚刚发现这颗宝石,所以我还不知道 .

    更新:设置我的API密钥后,我可以通过 create 命令在github上创建一个新的repo,但是我无法使用 create-from-local 命令,该命令应该采用当前的本地存储并使相应的远程控制输出github上 .

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

    如果有人对此有所了解,我'd love to know what I'我做错了 . 已经issue filed了 .

    更新:我最终做到了这一点 . 我不确定如何重新产生问题,但我刚从头开始(删除.git文件夹)

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

    现在这条线将创建远程仓库,甚至推动它,但不幸的是我不喜欢.1170214_d . 我希望它在github上被称为"dotfiles",但是gh gem只使用当前文件夹的名称,因为我在我的主文件夹中是"jason" . (我添加a ticket要求所需的行为)

    gh create-from-local
    

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

    gh create dotfiles
    
  • 3

    使用Bash Shell快速创建远程存储库

    每次创建存储库时键入完整代码都很麻烦

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

    更简单的方法是:

    • 在目录中创建一个shell脚本,即/ home / USER_NAME / Desktop / my_scripts,名为 githubscript.sh

    • 修改以下代码并将其保存到 githubscript.sh 文件中

    #!bin/bash
    curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
    git init;
    git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/$1.git;
    

    N.B. 这里 $1 是在保存脚本之前调用 script 更改 YOUR_GITHUB_USER_NAME 时作为 argument 传递的 repository name .

    • 设置 script 文件所需的权限 chmod 755 githubscript.sh

    • 在环境配置文件中包含scripts目录 . nano ~/.profile; export PATH="$PATH:$HOME/Desktop/my_scripts"

    • 还设置别名以运行githubscript.sh文件 . nano ~/.bashrc; alias githubrepo="bash githubscript.sh"

    • 现在重新加载终端中的 .bashrc.profile 文件 . source ~/.bashrc ~/.profile;

    • 现在创建一个新的存储库,即 demogithubrepo demo;

  • 2

    Nope, 你必须至少打开一次浏览器才能在GitHub上创建 username ,一旦创建,你可以利用GitHub API从命令行创建存储库,遵循以下命令:

    curl -u 'github-username' https://api.github.com/user/repos -d '{"name":"repo-name"}'
    

    For example:

    curl -u 'arpitaggarwal' https://api.github.com/user/repos -d '{"name":"command-line-repo"}'
    
  • 1

    我为GitHub和BitBucket使用REST API编写了一个名为 Gitter 的漂亮脚本:

    https://github.com/dderiso/gitter

    BitBucket:

    gitter -c -r b -l javascript -n node_app
    

    GitHub:

    gitter -c -r g -l javascript -n node_app
    
    • -c =创建新的回购

    • -r = repo provider(g = GitHub,b = BitBucket)

    • -n =命名回购

    • -l =(可选)在回购中设置应用程序的语言

  • -1

    我已经基于Bennedich's answer创建了一个Git别名来执行此操作 . 将以下内容添加到 ~/.gitconfig

    [github]
        user = "your_github_username"
    [alias]
        ; Creates a new Github repo under the account specified by github.user.
        ; The remote repo name is taken from the local repo's directory name.
        ; Note: Referring to the current directory works because Git executes "!" shell commands in the repo root directory.
        hub-new-repo = "!python3 -c 'from subprocess import *; import os; from os.path import *; user = check_output([\"git\", \"config\", \"--get\", \"github.user\"]).decode(\"utf8\").strip(); repo = splitext(basename(os.getcwd()))[0]; check_call([\"curl\", \"-u\", user, \"https://api.github.com/user/repos\", \"-d\", \"{{\\\"name\\\": \\\"{0}\\\"}}\".format(repo), \"--fail\"]); check_call([\"git\", \"remote\", \"add\", \"origin\", \"git@github.com:{0}/{1}.git\".format(user, repo)]); check_call([\"git\", \"push\", \"origin\", \"master\"])'"
    

    要使用它,请运行

    $ git hub-new-repo
    

    从本地存储库中的任何位置,并在提示时输入您的Github密码 .

  • 3

    For Rubyists:

    gem install githubrepo
    githubrepo create *reponame*
    

    根据提示输入用户名和密码

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

    资料来源:Elikem Adadevoh

  • 4

    基于@Mechanical Snail的其他答案,除了没有使用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"
    
  • 8

    有关创建令牌的说明,请转到here这是您要键入的命令(截至本答复日期 . (替换所有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"
    }
    

    你可以随时撤销你的令牌here

  • 2

    你需要的是hub . Hub是git的命令行包装器 . 它已经使用别名与本机git集成 . 它试图在git中提供github动作,包括创建新的存储库 .

    →  create a repo for a new project
    $ git init
    $ git add . && git commit -m "It begins."
    $ git create -d "My new thing"
    →  (creates a new project on GitHub with the name of current directory)
    $ git push origin master
    
  • 192

    出于代表原因,我不能将其添加为注释(最好用bennedich's answer),但对于Windows命令行,这里是正确的语法:

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

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

  • 14

    适用于所有Python 2.7 . *用户 . 那里是Github API的Python包装器,目前在版本3上,名为GitPython . 只需使用 easy_install PyGithubpip 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 对象文档是here .

  • 300

    Disclamier: I'm the author of the open source project

    支持此功能:https://github.com/chrissound/LinuxVerboseCommandLib本质上是这个脚本:

    #!/usr/bin/env bash
    
    # Create a repo named by the current directory
    # Accepts 1 STRING parameter for the repo description
    # Depends on bin: jq
    # Depends on env: GITHUB_USER, GITHUB_API_TOKEN
    github_createRepo() {
      projName="$(basename "$PWD")"
      json=$(jq -n \
        --arg name "$projName" \
        --arg description "$1" \
        '{"name":$name, "description":$description}')
    
      curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
      git init
      git remote add origin git@github.com:"$GITHUB_USER"/"$projName".git
      git push origin master
    };
    
  • 11

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

    你首先需要创建一个Github Personal Access Token

    在您喜欢的文本编辑器中打开〜/ .bash_profile或〜/ .bashrc . 在文件顶部附近添加以下行,其中导出的ed变量为:

    export 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用户帐户上创建新的公共远程存储库 .

  • 4

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

    我最近发现了create-github-repo . 从自述文件:

    安装:

    $ npm i -g create-github-repo
    

    用法:

    $ export CREATE_GITHUB_REPO_TOKEN=<access_token>
    $ create-github-repo --name "My coolest repo yet!"
    

    要么:

    $ create-github-repo <access_token> --name "My coolest repo yet!"
    
  • -1

    create a new repository on the command line

    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
    

    push an existing repository from the command line

    git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git
    
    git push -u origin master
    

相关问题