Skip to content

Instantly share code, notes, and snippets.

@salcode
Created September 5, 2015 19:54
Show Gist options
  • Save salcode/342391ccbaa8cbf48567 to your computer and use it in GitHub Desktop.
Save salcode/342391ccbaa8cbf48567 to your computer and use it in GitHub Desktop.
Notes for bash scripting git commands
# Current branch - Determine current git branch, store in $currentbranch, and exit if not on a branch
if ! currentbranch=$(git symbolic-ref --short -q HEAD)
then
echo We are not currently on a branch.
exit 1
fi
# Uncommited Changes - Exit script if there uncommited changes
if ! git diff-index --quiet HEAD --; then
echo "There are uncommited changes on this repository."
exit 1
fi
# Remote exists - Exit script if remote does not exist
git ls-remote --exit-code ${remote} >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Remote ${remote} does not exist"
exit 1
fi
# Root path of Repo
rootpath=$(git rev-parse --show-toplevel)
# Branch exists - Exit script if local branch does not exist
git rev-parse -q --verify ${branch} >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Branch ${branch} exists"
fi
@fenguoerbian
Copy link

Thank you, this is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment