目录

上传工程到代码托管平台

前言

在日常工作中经常会写一些工具类代码方便日后拷贝,将这部分代码上传到代码托管平台总是一件重复厌烦的工作,于是有了下面这两个脚本,分别对应于github托管平台和gitee托管平台。

脚本目标:

  1. 在代码托管平台创建工程名对应的仓库
  2. 本地工程根目录下git初始化
  3. 上传到远程仓库
  4. 复制markdown url到剪切板,方便记笔记

github

github-init

 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
#!/usr/bin/env bash

if [ -n "$1" ]
then
    repo_name=$1
else
    repo_name=`pwd | xargs basename`
fi 

username=lienhui68
api_token=github-api-token 

echo "set Repo name to ${repo_name}"
echo "创建远程仓库=======>${repo_name}"

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

echo "初始化本地仓库"
git init

echo "绑定远程仓库===>git@github.com:$username/$repo_name.git"
git remote add origin "git@github.com:$username/$repo_name.git"

echo "提交并上传代码中...."
git add .
git commit -m "$(date) -> 提交"
git push -u origin master
echo "代码上传到远程仓库${repo_name}======>ok"
echo "远程仓库地址: https://github.com/lienhui68/${repo_name}"

# 拼串markdown链接到剪切板,mac使用pbcopy,linux使用xsel
echo "[完整示例地址](https://github.com/lienhui68/${repo_name})" | pbcopy

gitee

gitee-init

 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
#!/usr/bin/env bash

if [ -n "$1" ]
then
    repo_name=$1
else
    repo_name=`pwd | xargs basename`
fi 

username=lienhui68
api_token=gitee-token 

echo "set Repo name to ${repo_name}"
echo "创建远程仓库=======>${repo_name}"

curl -X POST --header 'Content-Type: application/json;charset=UTF-8' 'https://gitee.com/api/v5/user/repos' -d '{"access_token":"'${api_token}'","name":"'$repo_name'","has_issues":"true","has_wiki":"true"}'

echo "初始化本地仓库"
git init

echo "绑定远程仓库===>git@gitee.com:$username/$repo_name.git"
git remote add origin "git@gitee.com:$username/$repo_name.git"

echo "提交并上传代码中...."
git add .
git commit -m "$(date) -> 提交"
git push -u origin master
echo "代码上传到远程仓库${repo_name}======>ok"
echo "远程仓库地址:https://gitee.com/lienhui68/${repo_name}"

# 拼串markdown链接到剪切板,mac使用pbcopy,linux使用xsel
echo "[完整示例地址](https://gitee.com/lienhui68/${repo_name})" | pbcopy

使用

在工程根目录下执行. github-init