Jaffer Sathick
Jaffer Sathick

Reputation: 546

How to create a new repo at Github using git bash?

How can I create a new repository from my machine using git bash?

I followed the below steps:

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

But I'm getting "Fatal error: did you run update-server-info on the server? "

Upvotes: 29

Views: 88654

Answers (8)

VladSavitsky
VladSavitsky

Reputation: 563

Overview

Command 'git' do not allow you to create repo but it's possible to create new repo at github from BASH script. All solutions use user/password authentication which is deplicated but stil in use. Authentication must be done using personal access token. Below there are solutions:

3rd party application

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>";

More options: https://hub.github.com/hub-create.1.html

Pure 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"

Upvotes: 1

tecnocrata
tecnocrata

Reputation: 891

First, try to do this right before the git push:

git pull repo branch

Then try to do the following:

$ git init --bare yourreponame.git

Then do what you were doing before:

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

Upvotes: 4

T04435
T04435

Reputation: 13992

I have created this bash file to do it all automatically.

#!/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 [email protected]:USERNAME/$reponame.git
git push -u origin master

So how:

copy the code above. save it as NAME.sh, add it to your PATH. restart terminal or open a new one.

$ NAME newreponame
$ NAME
$ Enter Github Repository Name: 

Thanks.

Upvotes: 9

Arunkumar Srisailapathi
Arunkumar Srisailapathi

Reputation: 1169

I think it is doable.

You can do it using curl (if you are on Windows, you'll have to install it)

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

Make sure to replace USER and REPO with your github username and the name of the repository you want to create respectively

It asks for password, input your github admin password and you are good to go.

Actually answered by James Barnett here https://teamtreehouse.com/community/how-does-one-add-a-repository-to-github-using-git-commandline-calls-only

Upvotes: 4

Laur
Laur

Reputation: 133

Maybe you are getting this error because you didn't set your identity:

$ git config --global user.name "John Doe" $ git config --global user.email [email protected]

Here you can find the steps to create and put your repository on github: http://programertools.blogspot.com/2014/04/how-to-use-github.html

Upvotes: 0

Akavall
Akavall

Reputation: 86128

Probably the easiest way to create a repo on github is somewhere before this line:

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

go to https://github.com/new and create a repository on github, then run your last two lines and everything should work.

Upvotes: 9

Edin Pjanic
Edin Pjanic

Reputation: 42

just try to add -u in your last line:

git push -u origin master

Upvotes: -1

Konarak
Konarak

Reputation: 283

You cannot create a repo on github using git bash. Git and github are different things. Github is a platform that let's you host and collaborate on code while git is the version control tool used. You can read more about them on wikipedia articles: github and git.

However if your intention is to create a github repo using terminal, you can do it using the github api and curl.

Upvotes: 27

Related Questions