【问题标题】:How to create a new repo at Github using git bash?如何使用 git bash 在 Github 创建一个新的仓库?
【发布时间】:2012-07-26 10:56:17
【问题描述】:

如何使用 git bash 从我的机器创建一个新的存储库?

我按照以下步骤操作:

mkdir ~/Hello-World

cd ~/Hello-World

git init

touch README

git add README

git commit -m 'first commit'

git remote add origin https://github.com/username/Hello-World.git

git push origin master

但我收到“致命错误:您是否在服务器上运行了 update-server-info?”

【问题讨论】:

标签: github github-api git-bash


【解决方案1】:

您不能使用 git bash 在 github 上创建存储库。 Git 和 github 是不同的东西。 Github 是一个让您托管和协作代码的平台,而 git 是使用的版本控制工具。您可以在 wikipedia 文章中阅读更多关于它们的信息:githubgit

但是,如果您打算使用终端创建 github 存储库,则可以使用 github api 和 curl 来完成。

【讨论】:

    【解决方案2】:

    在 github 上创建 repo 的最简单方法可能是在 这一行之前的某个地方:

    git remote add origin https://github.com/username/Hello-World.git
    

    转到https://github.com/new 并在 github 上创建一个存储库,然后运行您的最后两行代码,一切正常。

    【讨论】:

    • 有没有办法从终端创建一个新的 repo(在 github.com 上)?
    【解决方案3】:

    我已经创建了这个 bash 文件来自动完成这一切。

    #!/bin/sh
    reponame="$1"
    if [ "$reponame" = "" ]; then
    read -p "Enter Github Repository Name: " reponame
    fi
    mkdir ./$reponame
    cd $reponame
    curl -u USERNAME https://api.github.com/user/repos -d "{\"name\":\"$reponame\"}"
    git init
    echo "ADD README CONTENT" > README.md
    git add README.md
    git commit -m "Starting Out"
    git remote add origin git@github.com:USERNAME/$reponame.git
    git push -u origin master
    

    那么如何:

    复制上面的代码。将其保存为 NAME.sh,将其添加到您的 PATH 中。重新启动终端或打开一个新终端。

    $ NAME newreponame
    $ NAME
    $ Enter Github Repository Name: 
    

    谢谢。

    【讨论】:

    • 对不起,我来晚了一点;但我有一个问题。这会创建公共还是私有 GitHub 存储库?
    【解决方案4】:

    首先,尝试在 git push 之前执行此操作:

    git pull repo branch
    

    然后尝试执行以下操作:

    $ git init --bare yourreponame.git
    

    然后做你之前在做的事情:

    touch README
    
    git add README
    

    git commit -m 'first commit'

    git remote add origin https://github.com/username/Hello-World.git

    git push origin master

    【讨论】:

    • 我已经用 Postman 做了一个仓库,然后我使用命令git remote add origin <Url>,然后推送。谢谢。我想补充一点,命令中的单词顺序很重要!
    【解决方案5】:

    我认为这是可行的。

    您可以使用 curl 来完成(如果您在 Windows 上,则必须安装它)

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

    确保将 USER 和 REPO 分别替换为您的 github 用户名和您要创建的存储库的名称

    它要求输入密码,输入你的 github 管理员密码就可以了。

    James Barnett 在这里https://teamtreehouse.com/community/how-does-one-add-a-repository-to-github-using-git-commandline-calls-only实际回答了

    【讨论】:

      【解决方案6】:

      概述

      命令 'git' 不允许您创建 repo,但可以从 BASH 脚本在 github 创建新的 repo。所有解决方案都使用deplicated 但仍在使用中的用户/密码身份验证。必须使用personal access token 进行身份验证。 下面有解决办法:

      第三方应用程序

      https://github.com/github/hub

      sudo apt install hub;
      cd <folder with code>;
      hub init;
      hub create -p -d "<repo description>" -h "<project site>" \
      "user_name>/<repo_name>";
      

      更多选项:https://hub.github.com/hub-create.1.html

      纯 BASH

      REPONAME="TEST";
      DOMAIN="www.example.com";
      DESCRIPTION="Repo Description";
      GITHUB_USER="github_user";
      
      FOLDER="$HOME/temp/$REPONAME";
      mkdir -p "$FOLDER"; cd "$FOLDER";
      
      read -r -d '' JSON_TEMPLATE << EOF
      {
        "name" : "%s",
        "description" : "%s",
        "homepage" : "%s",
        "visibility" : "private",
        "private" : true,
        "has_issues" : false,
        "has_downloads" : false,
        "has_wiki" : false,
        "has_projects" : false
      }
      EOF
      
      JSON_OUTPUT=$(printf "$JSON_TEMPLATE" "$REPO_NAME" \
        "$DESCRIPTION" "http://$DOMAIN");
      
      # https://developer.github.com/v3/repos/#create-an-organization-repository
      curl -u ${GITHUB_USER} https://api.github.com/user/repos \
        -d "$JSON_OUTPUT"
      

      【讨论】:

        【解决方案7】:

        尝试在最后一行添加 -u:

        git push -u origin master
        

        【讨论】:

        【解决方案8】:

        也许您收到此错误是因为您没有设置身份:

        $ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com

        在这里您可以找到在 github 上创建和放置您的存储库的步骤: http://programertools.blogspot.com/2014/04/how-to-use-github.html

        【讨论】:

          猜你喜欢
          • 2022-06-24
          • 1970-01-01
          • 2020-12-04
          • 1970-01-01
          • 2014-03-24
          • 1970-01-01
          • 1970-01-01
          • 2011-10-19
          • 2021-12-08
          相关资源
          最近更新 更多