BASH

Some bash functions for git

Photo of Phillip KrügerPhillip KrügerJanuary 24th, 2019Last Updated: January 24th, 2019
0 385 2 minutes read

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 typeclone *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

Normalcommit, 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 ourSCG program. See the original article here:Some bash functions for git

Opinions expressed by System Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become asysadmin Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Tags
Photo of Phillip KrügerPhillip KrügerJanuary 24th, 2019Last Updated: January 24th, 2019
0 385 2 minutes read
Photo of 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.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.