git 提交偷懒方法

2017-07-04  本文已影响106人  学无止境吧

我从来就是个懒人。

使用git提交的时候,敲一堆命令,繁琐得很。
首先可以定义别名缩短键入的指令

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,这样还是没解决问题啊,还是很烦啊。
试试看能不能写个sh脚本,自动提交呢?
有想法就做他,于是有了下面的脚本。

#!/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 "执行:git checkout $G_GIT_MASTER_BRANCH"
            ret=$(git checkout $G_GIT_MASTER_BRANCH )
            #判断本地是否还有未提交的??
            echo "执行:git pull origin $G_GIT_MASTER_BRANCH"
            git pull origin $G_GIT_MASTER_BRANCH
            echo "执行:git checkout $G_GIT_BRANCH"
            git checkout $G_GIT_BRANCH
            echo "选择操作 1:Merge主分支 2:退出"
            read choose
            if [ $choose = "1" ];then
                echo "执行: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 "发现冲突"
                    exit 2
                fi
                echo "选择操作 1:提交到自己分支 2:退出"
                read choose
                if [ $choose = "1" ];then
                    echo "执行:git push origin $G_GIT_BRANCH"
                    git push origin $G_GIT_BRANCH
                    echo "结束,退出脚本"
                    exit 2  
                else
                    echo "退出脚本"
                    exit 2  
                fi
            else
                echo "退出脚本"
                exit 2  
            fi
        else
            echo "输入错误,退出脚本"
            exit 2  
        fi
    elif [ "$OUTTYPE" = "2" ]||[ "$OUTTYPE" = "6" ];then #
        git status
        echo "发现有修改"
        echo "选择操作 1:执行'git add -A' 2:退出手动处理"
        read choose
        if [ $choose = "1" ];then
           git add -A
        elif [ $choose = "2" ];then
            echo "手动提交,退出脚本"
            exit 2  
        else
            echo "输入错误,退出脚本"
            exit 2  
        fi
    elif [ "$OUTTYPE" = "3" ];then
        git status
        echo "发现有修改"
        echo "选择操作 1:执行'git add -A' 2:退出手动处理"
        read choose
        if [ $choose = "1" ];then
           git add -A
        elif [ $choose = "2" ];then
            echo "手动提交,退出脚本"
            exit 2  
        else
            echo "输入错误,退出脚本"
            exit 2  
        fi
    elif [ "$OUTTYPE" = "4" ];then
        echo "发现有修改"
        echo "选择操作 1:执行'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(修补bug)           \033[0m"
            echo "\033[32m:docs(文档documentation) \033[0m"
            echo "\033[32m:style(格式)            \033[0m"
            echo "\033[32m:refactor(重构)         \033[0m"
            echo "\033[32m:test(增加测试)       \033[0m"
            echo "\033[32m:chore(构建过程或辅助工具的变动) \033[0m"
            read commitType
            echo "输入您的commit文字"
            read commitStr
            git commit -m " $commitType commit:$commitStr"
            echo "已经 commit"

        elif [ $choose = "2" ];then
            echo "手动提交,退出脚本"
            exit 2  
        else
            echo "输入错误,退出脚本"
            exit 2  
        fi
    elif [ "$OUTTYPE" = "5" ];then
        echo "发现冲突!!!"
        exit 2  
    fi
}

while true;
do
    checkGitStatus
done

将脚本命名 autogit.sh,放在git目录。在ignore文件里面忽略本文件。
然后就可以用啦!

在目录直接敲 sh autogit.sh,然后一路敲 1,回车。

如果遇到每次都要求输入帐号密码,可以设置长久存储

git config --global credential.helper store
上一篇 下一篇

猜你喜欢

热点阅读