I created a new local Git repository:
~$ 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? I know it's no big deal to just fire up a browser and head over to Create a New Repository, but if there is a way to achieve this from the CLI I would be happy.
I read a vast amount of articles but none that I found mention how to create a remote repo from the CLI using git commands. Tim Lucas's nice article Setting up a new remote git repository is the closest I found, but GitHub does not provide shell access.
For all the Python 2.7.* users. There is a Python wrapper around the Github API that is currently on Version 3, called GitPython. Simply install using
easy_install PyGithub
orpip install PyGithub
.The
Repository
object docs are here.Disclamier: I'm the author of the open source project
This functionality is supported by: https://github.com/chrissound/LinuxVerboseCommandLib essentially it is this script:
Based on the other answer by @Mechanical Snail, except without the use of python, which I found to be wildly overkill. Add this to your
~/.gitconfig
:To Quickly Create the Remote Repository by Using a Bash Shell
It is cumbersome to type the complete code every time a repository is to be created
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
An easier approach is:
githubscript.sh
githubscript.sh
fileN.B. Here
$1
is therepository name
that is passed as anargument
when invoking thescript
ChangeYOUR_GITHUB_USER_NAME
before saving the script.Set required permissions to the
script
filechmod 755 githubscript.sh
Include the scripts directory in the environment configuration file.
nano ~/.profile; export PATH="$PATH:$HOME/Desktop/my_scripts"
Also set an alias to run the githubscript.sh file.
nano ~/.bashrc; alias githubrepo="bash githubscript.sh"
Now reload the
.bashrc
and.profile
files in the terminal.source ~/.bashrc ~/.profile;
Now to create a new repository i.e.
demo
:githubrepo demo;
For users with two-factor authentication, you can use bennedich's solution, but you just need to add the X-Github-OTP header for the first command. Replace CODE with the code that you get from the two-factor authentication provider. Replace USER and REPO with the username and name of the repository, as you would in his solution.
Nope, you have to open a browser atleast once to create your
username
on GitHub, once created, you can leverage GitHub API to create repositories from command line, following below command:For example: