如何使用git bash在Github上创建一个新的repo?

时间:2012-07-27 18:07:09

标签: github github-api git-bash

如何使用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?”

9 个答案:

答案 0 :(得分:23)

你不能使用git bash在github上创建一个repo。 Git和github是不同的东西。 Github是一个平台,让你主持和协作代码,而git是使用的版本控制工具。您可以在维基百科文章中详细了解它们:githubgit

但是如果您的目的是使用终端创建一个github repo,您​​可以使用github api和curl来完成。

答案 1 :(得分:7)

我已创建此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: 

感谢。

答案 2 :(得分:5)

在github上创建回购的最简单方法可能是之前这一行:

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

转到https://github.com/new并在github上创建一个存储库,然后运行最后两行,一切都应该有效。

答案 3 :(得分:3)

首先,尝试在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

答案 4 :(得分:2)

我认为这是可行的。

你可以使用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

回答

答案 5 :(得分:0)

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

git push -u origin master

答案 6 :(得分:0)

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

$ 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

答案 7 :(得分:0)

阅读GitHub Doc https://help.github.com/articles/create-a-repo/上的文档 他们做好了记录工作。

答案 8 :(得分:0)

概述

命令'git'不允许您创建存储库,但是可以从BASH脚本在github上创建新存储库。所有解决方案都使用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"