寫個git提交腳本,再也不用命令行了
平時項目中我們絕大部分都是用bash命令行,或者用GUI可視化工具,無論是小烏龜還是gui工具,如果是工具比較推薦sourceTree,但是我更推薦git-fork[1],工具因人而已,無論習慣命令行還是工具,尋得自己喜歡的方式就行,沒有好壞之分,也沒有高低之分。
如果你常常用gui,或者你常常用命令行,那么不妨用用腳本來解放你的雙手。
正文開始...
前置
正常情況下,我們知道我們bash中,我們使用git pull、git add .、git commit、git push等這些命令實際是在git bash環(huán)境下執(zhí)行的命令。相當于DOS環(huán)境或者shell執(zhí)行g(shù)it命令。
在git bash也是可以執(zhí)行.sh的xshell腳本
bash中的xshell命令
我們在bash新建一個index.sh文件測試一下
touch index.sh
在index.sh中輸入一段打印腳本
echo 'hello bash'
在命令行中輸入bash index.sh
刪除文件
我們在index.sh中新增一個命令
echo 'hello bash'
# 刪除test.txt
rm test.txt
# 刪除test目錄
rm -rf test
打開文件修改
# 打開xx文件修改
vim test2.txt
在終端中你需要用i插入,修改后執(zhí)行:wq!就可以保存退出了
查看目錄所有文件
ls -a
復制
# 將當前的test2.txt復制成test2_blank.txt
cp test2.txt test2_blank.txt
以上是一些常用的xshell命令,更多命令可以參考xshell[2]
git 提交本地代碼
以上基礎的了解一些常用的xshell命令,現(xiàn)在我們可以編寫一個xshell腳本了
首先我們在我們項目根目錄新建一個deplop.sh文件
touch deplop.sh
對應的deplop.sh
# 如果項目已經(jīng)初始化后,已經(jīng)init 那么不用加這個
# git init
# 更新your對應分支
git pull origin your_branch
# 查看本地狀態(tài)
git status
# 添加本地修改的文件
git add .
# 提交
git commit -m 'add xx'
# 添加遠程remote 如果項目已經(jīng)remote,可以省略
# git remote add origin https://github.com/xx.git
# 推送到指定分支
git push origin your_branch
然后我們在根目錄下創(chuàng)建一個package.json
npm init -y
然后在package.json中,添加命令
{
"name": "lessonNote",
"version": "1.0.0",
"description": "lessonNote-js 學習筆記",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"push": "bash deplop.sh"
},
...
}
然后我們運行命令npm run push
至此你就可以愉快的用一行命令了,但是正常情況下你每次得修改commit的信息,而不是寫死在deplop.sh腳本里面
于是你可以這樣
# 如果項目已經(jīng)初始化后,已經(jīng)init 那么不用
# git init
# 更新your_branch
git pull origin your_branch
# 查看本地狀態(tài)
git status
# 添加本地修改的文件
git add .
# 讀取終端輸入的信息
read -p "input your commit message: " msg
# 提交
git commit -m "$msg"
# 添加遠程remote 如果項目已經(jīng)remote,可以省略
# git remote add origin https://github.com/xx.git
# 推送到指定分支
git push origin your_branch
當你運行npm run push后,就會執(zhí)行上面你編輯的腳本,就可以快速的提交到自己倉庫了
如果你是想推一個你打包后的項目到指定倉庫,可以參考deplop.sh[3]
# deploy.sh
#!/usr/bin/env sh
# 確保腳本拋出遇到的錯誤
set -e
# 生成靜態(tài)文件
npm run build
# 進入生成的文件夾目錄
cd docs/.vuepress/dist
git init
# 添加當前文件
git add .
# 讀取終端輸入的信息
read -p "input commit message: " msg
git commit -m "$msg"
# remote 指定倉庫
git remote add origin https://github.com/xxx.git
# 推送到指定倉庫
git push -f origin your_branch
echo 'push success'
然后執(zhí)行npm run push這樣就可以一行命令替代你提交的所有操作了
總結(jié)
了解一些常用的xshell腳本命令,在xx.sh這樣的文件,你可以編寫一些腳本,對文件進行刪除,修改等操作
新建一個deplop.sh文件,編寫git提交本地文件,解放git add 、git commit、git push操作
本文示例code example[4]
參考資料
[1]
git-fork: https://git-fork.com/
[2]
xshell: https://cloud.tencent.com/developer/article/1778560
[3]
deplop.sh: https://github.com/maicFir/maicFir.github.io/blob/dev2.0/deploy.sh
[4]
code example: https://github.com/maicFir/lessonNote/tree/master/bash
作者:Maic
歡迎關注微信公眾號 :web技術(shù)學苑