平时项目中我们绝大部分都是用bash
命令行,或者用GUI
可视化工具,无论是小乌龟还是gui
工具,如果是工具比较推荐sourceTree
,但是我更推荐git-fork[1],工具因人而已,无论习惯命令行还是工具,寻得自己喜欢的方式就行,没有好坏之分,也没有高低之分。
如果你常常用gui
,或者你常常用命令行
,那么不妨用用脚本来解放你的双手。
正文开始...
正常情况下,我们知道我们bash
中,我们使用git pull
、git add .
、git commit
、git push
等这些命令实际是在git bash
环境下执行的命令。相当于DOS
环境或者shell
执行git
命令。
在git bash
也是可以执行.sh
的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
插入,修改后执行:wq!
就可以保存退出了
ls -a
# 将当前的test2.txt复制成test2_blank.txt
cp test2.txt test2_blank.txt
以上是一些常用的xshell
命令,更多命令可以参考xshell[2]
以上基础的了解一些常用的xshell
命令,现在我们可以编写一个xshell
脚本了
首先我们在我们项目根目录新建一个deplop.sh
文件
touch deplop.sh
对应的deplop.sh
# 如果项目已经初始化后,已经init 那么不用加这个
# git init
# 更新your对应分支
git pull origin your_branch
# 查看本地状态
git status
# 添加本地修改的文件
git add .
# 提交
git commit -m 'add xx'
# 添加远程remote 如果项目已经remote,可以省略
# git remote add origin https://github.com/xx.git
# 推送到指定分支
git push origin your_branch
然后我们在根目录下创建一个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
脚本里面
于是你可以这样
# 如果项目已经初始化后,已经init 那么不用
# git init
# 更新your_branch
git pull origin your_branch
# 查看本地状态
git status
# 添加本地修改的文件
git add .
# 读取终端输入的信息
read -p "input your commit message: " msg
# 提交
git commit -m "$msg"
# 添加远程remote 如果项目已经remote,可以省略
# git remote add origin https://github.com/xx.git
# 推送到指定分支
git push origin your_branch
当你运行npm run push
后,就会执行上面你编辑的脚本,就可以快速的提交到自己仓库了
如果你是想推一个你打包后的项目到指定仓库,可以参考deplop.sh[3]
# deploy.sh
#!/usr/bin/env sh
# 确保脚本抛出遇到的错误
set -e
# 生成静态文件
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'
然后执行npm run push
这样就可以一行命令替代你提交的所有操作了
xshell
脚本命令,在xx.sh
这样的文件,你可以编写一些脚本,对文件进行删除,修改等操作deplop.sh
文件,编写git
提交本地文件,解放git add
、git commit
、git push
操作[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