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

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 or pip install PyGithub.

from github import Github
g = Github(your-email-addr, your-passwd)
repo = g.get_user().user.create_repo("your-new-repos-name")

# Make use of Repository object (repo)

The Repository object docs are here.

查看更多
余生请多指教
3楼-- · 2019-01-01 06:30

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:

#!/usr/bin/env bash

# Create a repo named by the current directory
# Accepts 1 STRING parameter for the repo description
# Depends on bin: jq
# Depends on env: GITHUB_USER, GITHUB_API_TOKEN
github_createRepo() {
  projName="$(basename "$PWD")"
  json=$(jq -n \
    --arg name "$projName" \
    --arg description "$1" \
    '{"name":$name, "description":$description}')

  curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
  git init
  git remote add origin git@github.com:"$GITHUB_USER"/"$projName".git
  git push origin master
};
查看更多
裙下三千臣
4楼-- · 2019-01-01 06:31

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:

[github]
    user = "your-name-here"
[alias]
    hub-new-repo = "!REPO=$(basename $PWD) GHUSER=$(git config --get github.user); curl -u $GHUSER https://api.github.com/user/repos -d {\\\"name\\\":\\\"$REPO\\\"} --fail; git remote add origin git@github.com:$GHUSER/$REPO.git; git push origin master"
查看更多
回忆,回不去的记忆
5楼-- · 2019-01-01 06:32

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:

  1. create a shell script in a directory i.e. /home/USER_NAME/Desktop/my_scripts named githubscript.sh
  2. Modify and save the following code to the githubscript.sh file
#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/$1.git;

N.B. Here $1 is the repository name that is passed as an argument when invoking the script Change YOUR_GITHUB_USER_NAME before saving the script.

  1. Set required permissions to the script file chmod 755 githubscript.sh

  2. Include the scripts directory in the environment configuration file. nano ~/.profile; export PATH="$PATH:$HOME/Desktop/my_scripts"

  3. Also set an alias to run the githubscript.sh file. nano ~/.bashrc; alias githubrepo="bash githubscript.sh"

  4. Now reload the .bashrc and .profile files in the terminal. source ~/.bashrc ~/.profile;

  5. Now to create a new repository i.e. demo: githubrepo demo;

查看更多
明月照影归
6楼-- · 2019-01-01 06:33

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.

curl -u 'USER' -H "X-GitHub-OTP: CODE" -d '{"name":"REPO"}' https://api.github.com/user/repos
git remote add origin git@github.com:USER/REPO.git
git push origin master
查看更多
墨雨无痕
7楼-- · 2019-01-01 06:35

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:

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

For example:

curl -u 'arpitaggarwal' https://api.github.com/user/repos -d '{"name":"command-line-repo"}'
查看更多
登录 后发表回答