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.
Found this solution which I liked: https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564
You first need to create a Github Personal Access Token
Open up your ~/.bash_profile or ~/.bashrc in your favorite text editor. Add the following line near the top of your file, where the rest of the export ‘ed variables are:
export GITHUB_API_TOKEN=<your-token-here>
Somewhere below, by your other bash functions, you can paste something similar to the following:
Now, whenever you’re creating a new project, you can run the command
$ new-git awesome-repo
to create a new public remote repository on your Github user account.CLI commands for github API v3 (replace all CAPS keywords):
Simple steps (using
git
+hub
=> GitHub):Install Hub (GitHub).
brew install hub
go get github.com/github/hub
otherwise (having Go as well):
Go to your repo or create empty one:
mkdir foo && cd foo && git init
.Run:
hub create
, it'll ask you about GitHub credentials for the first time.Usage:
hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]
Example:
hub create -d Description -h example.com org_name/foo_repo
Then commit and push as usual or check
hub commit
/hub push
.For more help, run:
hub help
.See also: Importing a Git repository using the command line at GitHub.
There is an official github gem which, I think, does this. I'll try to add more information as I learn, but I'm only just now discovering this gem, so I don't know much yet.
UPDATE: After setting my API key, I am able to create a new repo on github via the
create
command, however I am not able to use thecreate-from-local
command, which is supposed to take the current local repo and make a corresponding remote out on github.If anyone has some insight on this, I'd love to know what I'm doing wrong. There's already an issue filed.
UPDATE: I did eventually get this to work. I'm not exactly sure how to re-produce the issue, but I just started from scratch (deleted the .git folder)
Now this line will create the remote repo and even push to it, but unfortunately I don't think I can specify the name of the repo I'd like. I wanted it to be called "dotfiles" out on github, but the gh gem just used the name of the current folder, which was "jason" since I was in my home folder. (I added a ticket asking for the desired behavior)
This command, on the other hand, does accept an argument to specify the name of the remote repo, but it's intended for starting a new project from scratch, i.e. after you call this command, you get a new remote repo that's tracking a local repo in a newly-created subfolder relative to your current position, both with the name specified as the argument.
For Rubyists:
enter username and pw as prompted
Source: Elikem Adadevoh
For directions on creating a token, go here This is the command you will type (as of the date of this answer. (replace all CAPS keywords):
Once you enter your password you will see the following which contains your token.
You can revoke your token anytime by going here