用命令在本地创建github仓库

问题

每次创建github仓库,都要到github官网,有点麻烦,想在本地直接创建github仓库,写好项目后直接push。
操作系统:linux

步骤

1, 首先在github申请一个私人api token。 申请地址:https://github.com/settings/tokens/new
申请时taken权限一般全部勾选。系统生成Token码,先复制保存到本地,注意再次打开就看不到了,如果忘了就从新生成。
而且如果自己项目里面包含该taken,则这个taken也会失效。

2, 配置.bashrc文件,写一个自动生成仓库的命令。在本机使用的bash的配置文件中加入下述函数定义。

github-create() 
{if [ -n "$1" ]
then
    repo_name=$1
else
    repo_name=`pwd | xargs basename`
fi 

echo "set Repo name to ${repo_name}"
curl -u 'username:api_token' https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'
git remote add origin git@github.com:username/$repo_name.git
}
**使用自己的username与api_token覆盖上述函数中相应的值。**

注意以下几点:
- [ $1 ],$1两侧必须有空格
- 配置了github的ssh登录

3, 重启配置文件 . ./.bashrc。 使用方法:github-create repo_namegithub-create(默认当前目录为仓库名),注意使用命令的当前目录下必须有本地仓库,即有.git文件夹在。


将本地代码仓库push到github远程代码仓库

 以下省去在本地创建git仓库以及提交commit等操作。

 (1)首先将本地仓库和远程代码仓库进行关联:

 git remote add origin your_repo_url.git

 (2)然后将本地代码仓库push到github:

 git push -u --set-upstream origin master

原文地址:https://www.cnblogs.com/friedCoder/p/12255041.html