Skip to content

Instantly share code, notes, and snippets.

@tiagojsag
Last active March 14, 2023 13:25
Show Gist options
  • Save tiagojsag/f2cfad87becbef2194f1ac7d085e9458 to your computer and use it in GitHub Desktop.
Save tiagojsag/f2cfad87becbef2194f1ac7d085e9458 to your computer and use it in GitHub Desktop.
All-in-one command to checkout, commit and push git changes
#!/usr/bin/env bash
########################
# GIT AIO
# Author: https://github.com/tiagojsag
#
# This command accepts two optional arguments:
# -b: if given, a ne branch is checked out. By default, it uses the current branch
# -m <message>: commit message. If not provided, you will be prompted for it
#
# It executes the following actions:
# - If -b provided, creates and checks out a new branch named after the commit message
# - Adds all unstaged files to staging
# - Commits the changes
# - Pushes the changes to "origin"
#########################
old_branch_name=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
checkout_new_branch=false
commit_message=false
while getopts bm: option
do
case "${option}"
in
b) checkout_new_branch=true;;
m) commit_message="$OPTARG";;
esac
done
if [ "$commit_message" = false ]
then
echo "Please enter your commit message: "
read commit_message
fi
if [ "$checkout_new_branch" = true ]
then
lower_commit_message="$(tr '[:upper:]' '[:lower:]' <<< "$commit_message")"
branch_name=${lower_commit_message//[[:space:]]/-}
printf '\nGIT AIO - Checking out new branch\n'
git checkout -b ${branch_name} || { printf 'git checkout failed' ; exit 1; }
else
branch_name=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
fi
printf '\nGIT AIO - Adding all files\n'
git add -A || { printf '\nGIT AIO - git add failed' ; exit 1; }
printf '\nGIT AIO - Committing\n'
git commit -am "${commit_message}" || { printf '\nGIT AIO - git commit failed' ; exit 1; }
printf '\nGIT AIO - Pushing\n'
git push --set-upstream origin ${branch_name} || { printf '\nGIT AIO - git push failed' ; exit 1; }
printf '\nGIT AIO - Finished\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment