Skip to content

Instantly share code, notes, and snippets.

@seanmhanson
Last active November 1, 2019 16:11
Show Gist options
  • Save seanmhanson/e94e35ece881abc2a1e6ef4120d0179f to your computer and use it in GitHub Desktop.
Save seanmhanson/e94e35ece881abc2a1e6ef4120d0179f to your computer and use it in GitHub Desktop.
Custom git command to push to a remote branch matching the local branch name
#!/bin/sh
while getopts ":hfl" opt; do
case ${opt} in
h ) echo "Usage: git push-up [-h] [-f] [-l]"
echo "[-f] push using --force"
echo "[-h] display help and usage (you are here)"
echo "[-l] push using --force-with-lease"
exit 0
;;
f ) force=true
;;
l ) lease=true
;;
\? ) echo "Usage: git push-up [-h] [-f] [-l]"
exit 0
;;
esac
done
if currentBranch=$(git symbolic-ref --short -q HEAD)
then
if [ "$force" = true ]; then
echo Force pushing local branch to origin/"$currentBranch"
git push origin "$currentBranch" --force
elif [ "$lease" = true ]; then
echo Pushing local branch to origin/"$currentBranch" with lease
git push origin "$currentBranch" --force-with-lease
else
echo Pushing local branch to origin/"$currentBranch"
git push origin "$currentBranch"
fi
else
echo "Error: the current branch could not be found"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment