Home » Shell Scripting » BASH » Some bash functions for git

About Phillip Krüger

Phillip Krüger
Phillip is a software developer and a systems architect who knacks for solving problems. He has a passion for clean code and evolutionary architecture. He blogs about all technical things.

Some bash functions for git

Here some git related functions in my .bachrc. Is mostly a backup for me, but it might also be useful for someone else.

Cloning a git repo

Because I usually clone repos from my github account, this is a shortcut that allows me to just type clone *repo_name* and it will create the URL.

function clone {

        if [ $# -eq 0 ]; then
            echo "Please enter repo name or full url:";
            read repo;
            clone $repo;
        elif [[ $1 == --help ]] || [[ $1 == --h ]] || [[ $1 == --? ]]; then
            echo "This will clone a git repo.";
            echo "";
            echo "Option 1: You can just provide the name, eg:";
            echo "$ clone membership";
            echo "This will do: git clone https://github.com/phillip-kruger/membership.git";
            echo "";
            echo "Option 2: Provide the full URL";
            echo "$ clone https://github.com/smallrye/smallrye-rest-client.git";
            echo "This will do: git clone https://github.com/smallrye/smallrye-rest-client.git";
        else    
            if [[ $1 == https://* ]] || [[ $1 == git://* ]] || [[ $1 == ssh://* ]] ; then
                URL=$1;
            else
                URL='https://github.com/phillip-kruger/'$1'.git';
            fi    

            echo git clone "$URL";
            git clone "$URL";
        fi
    }

    export -f clone

Usage:

clone *reponame* – this will go to my github account

bash functions

clone *url* – clone the repo at the url

bash functions

clone – will ask for the repo name or url

bash functions

Syncing your fork to upstream

If you contribute to projects, and you are working against your own fork, this is a handy way to keep you fork in sync with changes in the upstream master.

function sync {

        if git remote -v | grep -q 'upstream'; then
            echo "upstream exist";
        else
            echo "Please enter the upstream git url:";
            read url;
            git remote add upstream "$url"
        fi

        git remote -v
        git fetch upstream
        git pull upstream master
        git checkout master
        git rebase upstream/master
    }

    export -f sync
bash functions

Commit

Normal commit, but adding -s to include your signature.

function commit {

        if [ $# -eq 0 ]; then
            echo "Please enter a commit message:";
            read msg;
            commit "$msg";
        elif [[ $1 == --help ]] || [[ $1 == --h ]] || [[ $1 == --? ]]; then
            echo "This will commit changes to a local git repo, eg:";
            echo "$ commit 'some changes made'";
            echo "This will do: git commit -s -m 'some changes made'";
        else    
            echo git commit -s -a -m "$1"
            git commit -s -a -m "$1";
        fi
    }

    export -f commit
bash functions

Published on System Code Geeks with permission by Phillip Krüger, partner at our SCG program. See the original article here: Some bash functions for git

Opinions expressed by System Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of