前言
在日常开发工作中,尤其是本人这种游击式的老牛,使用 Git 工具,提交代码的时候每次都需要手敲 add、commit、pull、push 等命令,或者,使用图形界面的还需要手动点击对应按钮才行。时常忘记 pull代码、push错分支,所以比较苦恼。所以配置一些脚本(.sh)来执行提交、拉取等命令来快速操作。
.sh 是什么格式
是一种脚本文件格式,主要应用于Unix、Linux系统中运行shell命令。
脚本授权
sudo chmod +x /path/to/your/.add.sh
执行脚本
- 终端执行
cd /path/to/directory
./.add.sh
- 直接拖拽到编辑器的终端 => Enter
提交脚本(.add.sh)
#!/bin/bash
# Step 1: input the commit message
echo "Please enter the message of the submission content:"
read REASON
if [ "$REASON"x = ""x ]; then
echo "Please enter the message of the submission content:"
exit 0
fi
# Step 2: Get the current branch name
branch_name=$(git rev-parse --abbrev-ref HEAD)
if [ $? -ne 0 ]; then
echo "Failed to get the current branch name❌"
exit 1
fi
# Step 3: run git add .
git add .
# Step 4: commit the code
git commit -m "${REASON}"
if [ $? -ne 0 ]; then
echo "Commit failed❌"
exit 1
fi
# Step 5: Run git pull --rebase
git pull --rebase
if [ $? -ne 0 ]; then
echo "git pull --rebase failed❌"
exit 1
fi
# Step 6: Run git push origin HEAD:refs/for/branch_name
git push origin HEAD:refs/for/$branch_name
if [ $? -ne 0 ]; then
echo "git push origin HEAD:refs/for/$branch_name failed❌"
exit 1
fi
echo "git push origin HEAD:refs/for/$branch_name ✨ succeeded ✨ ✅"