分享一個git 縮寫腳本

分享一個git 縮寫腳本

git 簡寫命令,加入linux,mac別名
使用方法:
./gsh [命令名] [參數,可選]linux

set 設置用戶名和郵箱, 1:名稱, 2:郵箱
init 設置git 簡潔命令
gst: 代碼變化狀態,git status
gd: 查看當前代碼改動變化, git diff, git diff 分支1 分支2
gam: [gam 備註] 代碼提交本地倉庫: git commit -am "備註"
gpu: [gpu 遠程分支名] 與遠程倉庫關聯: git push -u origin 分支名
gp: [gp]推送到遠程倉庫: git push
gl: [gl]拉取遠程倉庫代碼:git pull
gb: [gb 新分支名]建立並切換新分支:git checkout -b 新分支名
gm: [gm 分支名]合併分支: git merge 分支名
gcm: [gcm 目標分支名]切換分支,並更新,再合併: git checkout 分支名B && git pull && git merge 分支名A
gsave 保存臨時修改文件, git stash save
gpop 恢復臨時修改的文件,git stash pop
glg 查看git日誌.git loggit

#!/usr/bin/env bash
#
#
#
#                                                    __----~~~~~~~~~~~------___
#                                   .  .   ~~//====......          __--~ ~~
#                   -.            \_|//     |||\\  ~~~~~~::::... /~
#                ___-==_       _-~o~  \/    |||  \\            _/~~-
#        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
#    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
#  .~       .~       |   \\ -_    /  /-   /   ||      \   /
# /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
# |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
#          '         ~-|      /|    |-~\~~       __--~~
#                      |-~~-_/ |    |   ~\_   _-~            /\
#                           /  \     \__   \/~                \__
#                       _--~ _/ | .-~~____--~-/                  ~~==.
#                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
#                                 -_     ~\      ~~---l__i__i__i--~~_/
#                                 _-~-__   ~)  \--______________--~~
#                               //.-~~~-~_--~- |-------~~~~~~~~
#                                      //.-~~~--\
#                               神龍保佑
#                              代碼無BUG!
#
###########################################################################################################
# git 簡寫命令,加入linux,mac別名
#
# 使用方法:
#     ./gsh [命令名] [參數,可選]
#
# set   設置用戶名和郵箱, 1:名稱, 2:郵箱
# init  設置git 簡潔命令
#
# gst:     代碼變化狀態,git status
# gd:     查看當前代碼改動變化, git diff, git diff 分支1 分支2
# gam:    [gam 備註] 代碼提交本地倉庫: git commit -am "備註"
# gpu:    [gpu 遠程分支名] 與遠程倉庫關聯: git push -u origin 分支名
# gp:     [gp]推送到遠程倉庫: git push
# gl:     [gl]拉取遠程倉庫代碼:git pull
# gb:      [gb 新分支名]建立並切換新分支:git checkout -b 新分支名
# gm:     [gm 分支名]合併分支: git merge 分支名
# gcm:     [gcm 目標分支名]切換分支,並更新,再合併: git checkout 分支名B && git pull && git merge 分支名A
# gsave 保存臨時修改文件, git stash save
# gpop  恢復臨時修改的文件,git stash pop
# glg    查看git日誌.git log
#
#
# 編輯時間: 2019-05-24, 做者: 百里, 小站: sgfoot.com, bbs.sgfoot.com
###############################################################################################################
cate=$1 #操做名稱
second=$2 # 第1個參數
third=$3 # 第2個參數

#默認值
default_name="百里"
default_email="sgfoot2020@gmail.com"

# 定義變量
tmp_file=/tmp/git.branch

# 獲取分支名
function git.branch {
  br=`git branch | grep "*"`
  touch /tmp/git.branch
  echo ${br/* /} > $tmp_file
}
# 初使值
function git.init {
    git config --global alias.st status
    git config --global alias.df diff
    git config --global alias.co checkout
    git config --global alias.cb 'checkout -b'
    git config --global alias.ci commit
    git config --global alias.br branch
    git config --global alias.mg  merge
    git config --global alias.save 'stash save'
    git config --global alias.pop 'stash pop'
    git config --global alias.un 'reset HEAD'
    git config --global alias.last 'log -1'
    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
    git config -l
}
# 顯示執行狀態
function showStatus {
    if [ $? -eq 0 ];then
        echo "執行成功"
    else
        echo "執行失敗"
    fi
}
# 顯示最近1次提交詳情
function showLast {
    count=$1
    show_count=${count:-1}
    git log -$show_count --stat --graph --oneline --relative-date --abbrev-commit -p --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
}
# 簡潔方式顯示日誌
function showLog {
    git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
}
#對zsh進行別名新建
function create_zshrc {
    #複製當前腳本 到 /usr/local/sbin裏
    gsh_file=/usr/local/sbin/gsh
    basepath=$(cd `dirname $0`; pwd)
    filename=$(basename $0)
    gsh=${basepath}/${filename}

    git_alias=~/.zshrc
    #check
    is=`cat $git_alias |grep "alias gst"`
    if [ -n "$is" ];then
        echo "已經安裝過"
        exit 0
    fi

    echo "alias gst='$gsh gst'"     >> $git_alias
    echo "alias gam='$gsh gam'"      >> $git_alias
    echo "alias gd='$gsh gd'"     >> $git_alias
    echo "alias gpu='$gsh gpu'"     >> $git_alias
    echo "alias gp='$gsh gp'"     >> $git_alias
    echo "alias gl='$gsh gl'"     >> $git_alias
    echo "alias gb='$gsh gb'"     >> $git_alias
    echo "alias gm='$gsh gm'"     >> $git_alias
    echo "alias gcm='$gsh gcm'"     >> $git_alias
    echo "alias gsave='$gsh gsave'"     >> $git_alias
    echo "alias gpop='$gsh gpop'"     >> $git_alias
    echo "alias glg='$gsh glg'"     >> $git_alias
    echo "alias gset='$gsh gset'"     >> $git_alias
}
#對.bash_profile進行別名新建
function create_profile {
        #複製當前腳本 到 /usr/local/sbin裏
        gsh_file=/usr/local/sbin/gsh
        basepath=$(cd `dirname $0`; pwd)
        filename=$(basename $0)
        gsh=${basepath}/${filename}

        git_alias=~/.bash_profile
    #check
        is=`cat $git_alias |grep "alias gst"`
        if [ -n "$is" ];then
                echo "已經安裝過"
                exit 0
        fi

        echo "alias gst='$gsh gst'"      >> $git_alias
    echo "alias gam='$gsh gam'"     >> $git_alias
        echo "alias gd='$gsh gd'"        >> $git_alias
        echo "alias gpu='$gsh gpu'"      >> $git_alias
        echo "alias gp='$gsh gp'"        >> $git_alias
        echo "alias gl='$gsh gl'"        >> $git_alias
        echo "alias gb='$gsh gb'"        >> $git_alias
        echo "alias gm='$gsh gm'"        >> $git_alias
        echo "alias gcm='$gsh gcm'"      >> $git_alias
        echo "alias gsave='$gsh gsave'"  >> $git_alias
        echo "alias gpop='$gsh gpop'"    >> $git_alias
        echo "alias glg='$gsh glg'"      >> $git_alias
        echo "alias gset='$gsh gset'"    >> $git_alias
}

help() {
    if [ "$1" = "h" ];then
        echo $2
        exit 0
    fi
}



# case進行操做
case $cate in
    create) # 建立文件
        if [ $2 -eq 1 ];then
            echo "對bash_profile進行別名新建"
            create_profile && showStatus
        else
            echo "對zshrc進行別名新建"
            create_zshrc && showStatus
        fi
    ;;
    init) #初使化
        echo "正在初使git簡潔命令"
        git.init && showStatus
    ;;
    set)
        help $second "正在設置git提交的用戶名和郵箱地址"
        echo "正在設置git提交的用戶名和郵箱地址"
        git config --global user.name $second
        git config --globaluser.email $third
        showStatus
    ;;
    my)
                help $second "正在設置git提交的用戶名和郵箱地址"
                echo "正在設置git提交的用戶名和郵箱地址"
                git config user.name $default_name
                git config user.email $default_email
                showStatus
        ;;
    gst) # 查看狀態
        git status && echo $?
    ;;
    gf) # 對比變化, 支持版本對比,例 gf 須要對比的分支名(注:綠色字體是添加的代碼,紅色是刪除的代碼)
        if [ "$second" = "h" ];then
            echo "對比變化, 支持版本對比,例 gf 須要對比的分支名(注:綠色字體是添加的代碼,紅色是刪除的代碼)"
            exit 0
        fi
        if [ -n "$second" ];then
            git.branch
            local_branch=`cat $tmp_file`
            git diff $second $local_branch
        else
            git diff
        fi
    ;;
    gam) # 添加,提交代碼到本地
        if [ -z "$second" ];then
            echo "請寫上你精彩的備註,如: 這是一代牛逼烘烘的代碼!!!, 代號:(nb)"
            exit 0
        fi
        if [ "$second" = "nb" ];then
            second="fix:TODO This is nb code"
        fi
        git add -A && git commit -m "$second" && showLast && showStatus
    ;;
    gpu) # 提交代碼遠程並關聯遠程倉庫
        git push -u origin $second && showStatus
    ;;
    gp) # 提交代碼到遠程
        if [ -n "$second" ];then
            git push origin $second && showStatus
        else
            git push && showStatus
        fi
    ;;
    gl) # 拉取代碼
        if [ -n "$second" ];then
            git pull origin $second && showStatus
        else
            git pull && showStatus
        fi
    ;;
    gb) # 建立並切換新分支
        git checkout -b $second && showStatus
    ;;
    gm) # 合併代碼
        if [ -z "$second" ];then
            git.branch
            current_branch=`cat $tmp_file`
            echo "請填寫須要合併的分支名稱,eg: 如填寫A分支名, Usage: git merge A ,表明A分支合併到$current_branch"
            exit 0
        fi
        git merge $second && showStatus
    ;;
    gcm) # 切換分支,併合並代碼
        if [ -z "$second" ];then
            echo "請跟上須要合併的分支名稱"
                        exit 0
        fi
        git.branch
        old_branch=`cat $tmp_file`
        git checkout $second && git pull && git merge $old_branch && git push && git checkout $old_branch && showStatus
    ;;
    glg) # 簡潔方式查看日誌
        showLog
    ;;
    gg) # 查看日誌哪些文件被改變
        git log  --graph --stat --color --abbrev-commit --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
    ;;
    gcode) # 查看日誌修改的具體的代碼
        git log --stat --graph --oneline --relative-date --abbrev-commit -p --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'
    ;;
    gsave) # 暫存項目修改
        git stash save && showStatus
    ;;
    gpop) # 取出暫存的數據
        git stash pop && showStatus
    ;;
    *)
        echo "Usage $0 {init|set|  gst|gam|gpu|gp|gl|gb|gm|gcm|glg|gg|gcode|gsave|gpop}"
        exit 1
    ;;
esac
相關文章
相關標籤/搜索