#!/bin/sh# @author Hubal
# @Email Hubal@123.com
# @createBy2018-11-30# Shell脚本提交git代码 简单,快速,高效
#
author=Hubalecho '>>>>>> start push <<<<<
echo "====== 当前分支 ======"branch=git branchecho$branch
# 判断参数1是否包含参数2
contains_str(){
#echo ">>> $1 <<
elsereturn0
fi}
git_add(){echo ">>>>>> 执行 git add 之前,本地文件状态如下 <<<<<
statusResult=$(git status)
no_change="nothing to commit"contains_str"$statusResult" "$no_change"
if [[ $? == 1 ]]; then
echo "=== 当前没有新增或者修改的文件 ==="git_push
exitfiread-p "是否确定add?Y|N :"add_paramsif [[ $add_params == "Y" || $add_params == "y" ]]; thengit add .elseexitfi}
git_commit(){echo ">>>>>> 执行 git commit 之前,本地文件状态如下 <<<<<
read-p "是否确定commit?Y|N :"commit_paramsif [[ $commit_params == "Y" || $commit_params == "y" ]] ; thenread-p "请输入commit信息:"commit_msgif [ -z $commit_msg ] ; thengit commit-m "git commit by $author".elsegit commit-m $commit_msg .fi
elif [[ $commit_params == "N" || $commit_params == "n" ]] ; thenexitelseexitfi}
git_push(){echo ">>>>>> 执行 git push 之前,本地文件状态如下 <<<<<
current_branch=$(git symbolic-ref --short -q HEAD)echo ">>>>>> 当前分支:$current_branch <<<<<
echo ">>>>>> end push <<<<<
push_result="";if [[ -z $origin_params && -z $branch_params ]]; then
echo ">>>>>> push origin $current_branch"
sleep 5git push origin $current_branchelif [[ -n $origin_params && -n $branch_params ]]; then
echo ">>>>>> push $origin_params $branch_params"
sleep 5git push $origin_params $branch_paramselif [[ -z $origin_params && -n $branch_params ]]; then
echo ">>>>>> push origin $branch_params"
sleep 5git push origin $branch_paramselif [[ -n $origin_params && -z $branch_params ]]; then
echo ">>>>>> push $origin_params $current_branch"
sleep 5git push $origin_params $current_branchelse
echo ">>>>>> end push <<<<<
fi}
read-p "默认push当前分支,Q代表quit,其他单词代表切换分支 :"branchif [[ $branch == "Y" || $branch == "y" || -z $branch ]] ; then#echo "你输入的是: $branch"statusResult=$(git status)
to_commit="Changes to be committed"contains_str"$statusResult" "$to_commit"
if [[ $? != 1 ]]; thengit_add;elsegit add .echo "====== 本地没有需要add的文件,直接commit ======"
figit_commit;
git_push;
exit;elif [[ $branch == "Q" || $branch == "q" ]] ; then#echo "你输入的是: $branch ,代表退出当前操作!"exitelsegit checkout $branchecho -e "当前分支: \n $(git branch)"git_add;
git_commit;
git_push;
exit;fi