摘要: 不需要在github网页上创建仓库,直接用命令行搞定,此文介绍三种直接在命令行创建GitHub仓库的形式!

准备工作

  1. 进入一个本地仓库,并初始化

    1
    
    git init && git add . && git commit -m "Init"
    1. 新建一个API Token 进入github - settings - Personal access tokens,generate new token,写入description,选择scopes(权限范围)。记住personal access token(那串数字,只显示一遍!),请记住它,下次就看不到了!

    一、命令行格式

    这是最直接的一种形式,直接把参数写到命令行搞定:

    1
    
    curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'

Note: 这里需要把$username$token分别换成实际的用户名和刚刚得到的personal access token,把$repo_name换成任何想要的repo name.

二、bash形式

可以把命令行写成bash脚本,下次只要执行里面的简单命令就可以执行以上整条命令。

  1. usernametoken写入~/.gitconfig,形式如下:

    1
    2
    3
    
    [github]
        user = your user name
        token = the token you get
    1. 把如下的bash code写入~/.bashrc或者~/.bash_profile文件:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    github-create() {
      repo_name=$1
         
      dir_name=`basename $(pwd)`
         
      if [ "$repo_name" = "" ]; then
        echo "Repo name (hit enter to use '$dir_name')?"
        read repo_name
      fi
         
      if [ "$repo_name" = "" ]; then
        repo_name=$dir_name
      fi
         
      username=`git config github.user`
      if [ "$username" = "" ]; then
        echo "Could not find username, run 'git config --global github.user <username>'"
        invalid_credentials=1
      fi
         
      token=`git config github.token`
      if [ "$token" = "" ]; then
        echo "Could not find token, run 'git config --global github.token <token>'"
        invalid_credentials=1
      fi
         
      if [ "$invalid_credentials" == "1" ]; then
        return 1
      fi
         
      echo -n "Creating Github repository '$repo_name' ..."
      curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' > /dev/null 2>&1
      echo " done."
         
      echo -n "Pushing local code to remote ..."
      git remote add origin git@github.com:$username/$repo_name.git > /dev/null 2>&1
      git push -u origin master > /dev/null 2>&1
      echo " done."
    }
  2. 使上述命令生效:重新打开或新启动一个Terminal,或者运行source ~/.bashrc

  3. 以后便可以使用如下命令创建远程仓库了:

    1
    
    github-create [repo_name]

    其中,默认的repo名是当前目录名。

    三、Bash形式简化版

    1. 把如下code写入~/.bashrc:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    github-create() 
    {if [ $1 ]
    then
    repo_name=$1
    else
    echo "Repo name?"
    read repo_name
    fi 
    curl -u '$username:$token' https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'
    git remote add origin git@github.com:efatsi/$repo_name.git
    git push -u origin master
    }
  4. 同Bash方式的步骤3

  5. 执行命令

    1
    
    simple-create [repo_name]

    四、备注

    本文转自这里,有修改。