25

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

Lahiru
  • 1,428
  • 13
  • 17
Jaffer Sathick
  • 466
  • 1
  • 10
  • 21
  • Duplicate http://stackoverflow.com/a/27456812/874188 suggests that there is (now?) an API for this. – tripleee Dec 13 '14 at 08:26

8 Answers8

26

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.

Konarak
  • 273
  • 3
  • 7
8

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.

Akavall
  • 82,592
  • 51
  • 207
  • 251
7

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 git@github.com: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.

T04435
  • 12,507
  • 5
  • 54
  • 54
  • 1
    Sorry I'm a little late to the party; but I have one question. Does this create a public or a private GitHub repository? – DarthVlader Jan 05 '21 at 14:29
4

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

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
tecnocrata
  • 901
  • 1
  • 8
  • 19
  • I've made a repository with Postman, and then I used the command `git remote add origin `, then push. Thanks. I want to add that the order of the words in the command is important ! – mihkov Jun 15 '17 at 19:41
4

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

1

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"
VladSavitsky
  • 523
  • 5
  • 13
0

just try to add -u in your last line:

git push -u origin master
  • 3
    This will not work for creating a new repo. -u is short for --set-upstream. It's explained pretty well here: http://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time – alexbhandari Dec 23 '13 at 05:30
0

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 johndoe@example.com

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

Laur
  • 133
  • 1
  • 6