我從來就是個(gè)懶人呻逆。
使用git提交的時(shí)候夸赫,敲一堆命令,繁瑣得很咖城。
首先可以定義別名縮短鍵入的指令
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.df diff
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.mg merge
btw茬腿,這樣還是沒解決問題啊,還是很煩啊宜雀。
試試看能不能寫個(gè)sh腳本滓彰,自動(dòng)提交呢?
有想法就做他州袒,于是有了下面的腳本揭绑。
#!/bin/sh
#定義變量
# 用戶自己分支
G_GIT_BRANCH="your_branch"
G_GIT_MASTER_BRANCH="master"
G_GIT_PATH=$(pwd)
echo "\033[32m 路徑:$G_GIT_PATH \033[0m"
echo "\033[32m 分支:$G_GIT_BRANCH \033[0m"
cd $G_GIT_PATH
function checkGitStatus
{
STR1="nothing to commit, working tree clean"
STR2="no changes added to commit"
STR3="Changes not staged for commit"
STR4="Changes to be committed"
STR5="both modified: "
STR6="Untracked files:"
OUTTYPE=-1
out=$(git status)
result=$(echo $out | grep "$STR5")
if [[ "$result" != "" ]];then
OUTTYPE=5
fi
if [ "$OUTTYPE" == "-1" ];then
result=$(echo $out | grep "$STR1")
if [[ "$result" != "" ]];then
OUTTYPE=1
fi
fi
if [ "$OUTTYPE" == "-1" ];then
result=$(echo $out | grep "$STR6")
if [[ "$result" != "" ]];then
OUTTYPE=6
fi
fi
if [ "$OUTTYPE" == "-1" ];then
result=$(echo $out | grep "$STR2")
if [[ "$result" != "" ]];then
OUTTYPE=2
fi
fi
if [ "$OUTTYPE" == "-1" ];then
result=$(echo $out | grep "$STR3")
if [[ "$result" != "" ]];then
OUTTYPE=3
fi
fi
if [ "$OUTTYPE" == "-1" ];then
result=$(echo $out | grep "$STR4")
if [[ "$result" != "" ]];then
OUTTYPE=4
fi
fi
if [ "$OUTTYPE" == "-1" ];then
result=$(echo $out | grep "$STR5")
if [[ "$result" != "" ]];then
OUTTYPE=5
fi
fi
if [ "$OUTTYPE" == "1" ];then
git status
echo "本地沒什么可提交的了."
echo "選擇操作 1:Pull master 2:退出"
read choose
if [ $choose = "1" ];then
echo "執(zhí)行:git checkout $G_GIT_MASTER_BRANCH"
ret=$(git checkout $G_GIT_MASTER_BRANCH )
#判斷本地是否還有未提交的??
echo "執(zhí)行:git pull origin $G_GIT_MASTER_BRANCH"
git pull origin $G_GIT_MASTER_BRANCH
echo "執(zhí)行:git checkout $G_GIT_BRANCH"
git checkout $G_GIT_BRANCH
echo "選擇操作 1:Merge主分支 2:退出"
read choose
if [ $choose = "1" ];then
echo "執(zhí)行:git merge $G_GIT_MASTER_BRANCH"
ret=$(git merge $G_GIT_MASTER_BRANCH)
#TODO:檢查是否有沖突
kstr="conflicts" #沖突
result=$(echo $ret | grep "$kstr")
if [[ "$result" != "" ]];then
echo "發(fā)現(xiàn)沖突"
exit 2
fi
echo "選擇操作 1:提交到自己分支 2:退出"
read choose
if [ $choose = "1" ];then
echo "執(zhí)行:git push origin $G_GIT_BRANCH"
git push origin $G_GIT_BRANCH
echo "結(jié)束,退出腳本"
exit 2
else
echo "退出腳本"
exit 2
fi
else
echo "退出腳本"
exit 2
fi
else
echo "輸入錯(cuò)誤郎哭,退出腳本"
exit 2
fi
elif [ "$OUTTYPE" = "2" ]||[ "$OUTTYPE" = "6" ];then #
git status
echo "發(fā)現(xiàn)有修改"
echo "選擇操作 1:執(zhí)行'git add -A' 2:退出手動(dòng)處理"
read choose
if [ $choose = "1" ];then
git add -A
elif [ $choose = "2" ];then
echo "手動(dòng)提交他匪,退出腳本"
exit 2
else
echo "輸入錯(cuò)誤,退出腳本"
exit 2
fi
elif [ "$OUTTYPE" = "3" ];then
git status
echo "發(fā)現(xiàn)有修改"
echo "選擇操作 1:執(zhí)行'git add -A' 2:退出手動(dòng)處理"
read choose
if [ $choose = "1" ];then
git add -A
elif [ $choose = "2" ];then
echo "手動(dòng)提交夸研,退出腳本"
exit 2
else
echo "輸入錯(cuò)誤邦蜜,退出腳本"
exit 2
fi
elif [ "$OUTTYPE" = "4" ];then
echo "發(fā)現(xiàn)有修改"
echo "選擇操作 1:執(zhí)行'git commit -m 備注' 2:退出"
read choose
if [ $choose = "1" ];then
echo "\033[31m 輸入提交類型: \033[0m"
echo "\033[32m:feat(新功能feature) \033[0m"
echo "\033[32m:fix(修補(bǔ)bug) \033[0m"
echo "\033[32m:docs(文檔documentation) \033[0m"
echo "\033[32m:style(格式) \033[0m"
echo "\033[32m:refactor(重構(gòu)) \033[0m"
echo "\033[32m:test(增加測(cè)試) \033[0m"
echo "\033[32m:chore(構(gòu)建過程或輔助工具的變動(dòng)) \033[0m"
read commitType
echo "輸入您的commit文字"
read commitStr
git commit -m " $commitType commit:$commitStr"
echo "已經(jīng) commit"
elif [ $choose = "2" ];then
echo "手動(dòng)提交,退出腳本"
exit 2
else
echo "輸入錯(cuò)誤亥至,退出腳本"
exit 2
fi
elif [ "$OUTTYPE" = "5" ];then
echo "發(fā)現(xiàn)沖突!!!"
exit 2
fi
}
while true;
do
checkGitStatus
done
將腳本命名 autogit.sh,放在git目錄悼沈。在ignore文件里面忽略本文件。
然后就可以用啦姐扮!
在目錄直接敲 sh autogit.sh,然后一路敲 1絮供,回車。
如果遇到每次都要求輸入帳號(hào)密碼茶敏,可以設(shè)置長(zhǎng)久存儲(chǔ)
git config --global credential.helper store