Is it possible to create a remote repo on GitHub f

2019-01-01 06:13发布

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.

22条回答
倾城一夜雪
2楼-- · 2019-01-01 06:37

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:

function new-git() {
    curl -X POST https://api.github.com/user/repos -u <your-username>:$GITHUB_API_TOKEN -d '{"name":"'$1'"}'
}

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.

查看更多
不流泪的眼
3楼-- · 2019-01-01 06:40

CLI commands for github API v3 (replace all CAPS keywords):

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin git@github.com:USER/REPO.git
git push origin master
查看更多
泪湿衣
4楼-- · 2019-01-01 06:42

Simple steps (using git + hub => GitHub):

  1. Install Hub (GitHub).

    • OS X: brew install hub
    • having Go: go get github.com/github/hub
    • otherwise (having Go as well):

      git clone https://github.com/github/hub.git && cd hub && ./script/build
      
  2. Go to your repo or create empty one: mkdir foo && cd foo && git init.

  3. 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

    Hub will prompt for GitHub username & password the first time it needs to access the API and exchange it for an OAuth token, which it saves in ~/.config/hub.

    To explicitly name the new repository, pass in NAME, optionally in ORGANIZATION/NAME form to create under an organization you're a member of.

    With -p, create a private repository, and with -d and -h set the repository's description and homepage URL, respectively.

    To avoid being prompted, use GITHUB_USER and GITHUB_PASSWORD environment variables.

  4. 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.

查看更多
心情的温度
5楼-- · 2019-01-01 06:42

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 the create-from-local command, which is supposed to take the current local repo and make a corresponding remote out on github.

$ gh create-from-local
=> error creating repository

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)

git init
git add .emacs
git commit -a -m "adding emacs"

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)

gh create-from-local

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.

gh create dotfiles
查看更多
梦该遗忘
6楼-- · 2019-01-01 06:43

For Rubyists:

gem install githubrepo
githubrepo create *reponame*

enter username and pw as prompted

git remote add origin *ctrl v*
git push origin master

Source: Elikem Adadevoh

查看更多
何处买醉
7楼-- · 2019-01-01 06:43

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):

curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations

Once you enter your password you will see the following which contains your token.

{
  "app": {
    "name": "YOUR_NOTE (API)",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  },
  "note_url": null,
  "note": "YOUR_NOTE",
  "scopes": [
    "repo"
  ],
  "created_at": "2012-10-04T14:17:20Z",
  "token": "xxxxx",
  "updated_at": "2012-10-04T14:17:20Z",
  "id": xxxxx,
  "url": "https://api.github.com/authorizations/697577"
}

You can revoke your token anytime by going here

查看更多
登录 后发表回答