Git commit bash script

后端 未结 6 2168
刺人心
刺人心 2020-12-25 10:53

I\'m writing a bash script to add, commit, push all files in a directory.

#!/bin/bash  
git add .  
read -p \"Commit description: \" desc  
git commit -m $de         


        
6条回答
  •  无人及你
    2020-12-25 11:10

    The following is a script that I use to mange my git repos - this will include the option to push to your origin branch, your staging site ( if setup ), and your production site ( if setup )

    #!/usr/bin/env bash
    
    # die script -- just in case
    die() { echo "$@" 1>&2 ; exit 1; }
    
    # kill message when dead 
     KILL="Invalid Command"
    
    # function to see where to push what branch
    pushing() {
        git branch
        sleep 1
        tput setaf 1;echo  What Branch?;tput sgr0 
        read -r branch
        tput setaf 2;echo  Where to? You can say 'origin', 'staging', or 'production';tput sgr0 
        read -r ans
        if [ "$ans" = "origin" ] || [ "$ans" = "staging" ] || [ "$ans" = "production" ]
        then
            git push "$ans" "$branch" 
        elif [ "$ans" = "no" ]
        then
            echo "Okay" 
        else die "$KILL"
        fi
    }
    
    # function to see how many more times
    more() {
        tput setaf 2;echo More?;tput sgr0 
        read -r more
        if [ "$more" = "yes" ]
        then
            pushing
        elif [ "$more" = "no" ]
        then
            die "Goodbye" 
        else die "$KILL"
        fi
    }
    
    # get the root directory in case you run script from deeper into the repo
    gr="$(git rev-parse --show-toplevel)"
    cd "$gr" || exit
    tput setaf 5;pwd;tput sgr0 
    
    # begin commit input
    git add . -A
    read -r -p "Commit description: " desc  
    git commit -m "$desc"
    
    # find out if we're pushin somewhere
    tput setaf 2;echo  wanna do some pushin?;tput sgr0 
    read -r push 
    if [ "$push" = "yes" ]
    then 
        pushing # you know this function 
        until [ "$more" = "no" ]
        do
            more # you know this function
        done
    elif [ "$push" = "no" ]
    then
        echo "Okay" 
    else die "$KILL"
    fi
    

    I tried to include as many comments as possible to help you understand what everything does.

    let me know if you have any questions.

    also, i have this setup like this

    echo "alias commit='sh /path/to/script.sh" >> ~/.bash_profile source ~/.bash_profile

    maybe this can help someone looking to accelerate workflow

提交回复
热议问题