Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
Last active December 21, 2015 01:48
Show Gist options
  • Save henrahmagix/6229954 to your computer and use it in GitHub Desktop.
Save henrahmagix/6229954 to your computer and use it in GitHub Desktop.
Automatically push current branch to remote and track at the same time.
# Call `git push --set-upstream` with current branch to a remote, defaulting to origin.
# Pass the remote as the first argument after the alias.
# Ensure confirm.sh is in your path.
# Usage: git psu [remote]
# Example:
# git remote add new-remote git@url...
# git checkout -b my-branch
# git psu new-remote
# -> Push new branch my-branch to new-remote?
# # Type any combination of yes, sure, ok
# -> git push --set-upstream new-remote my-branch
# Inline
git config alias.psu '![[ "" != $(git symbolic-ref HEAD 2>/dev/null) ]] && branch=$(git rev-parse --abbrev-ref HEAD) && remote=${1:-origin} && source confirm.sh && confirm "Push new branch $branch to $remote?" && git push --set-upstream $remote $branch'
# In gitconfig
[alias]
psu = ![[ '' != $(git symbolic-ref HEAD 2>/dev/null) ]] && branch=$(git rev-parse --abbrev-ref HEAD) && remote=${1:-origin} && source confirm.sh && confirm "Push new branch $branch to $remote?" && git push --set-upstream $remote $branch
# ======================================================================
#
# @link http://wuhrr.wordpress.com/2010/01/13/adding-confirmation-to-bash
#
# Function: confirm
# Asks the user to confirm an action, If the user does not answer yes,
# then the script will immediately exit.
#
# Parameters:
# $@ - The confirmation message
#
# Examples:
# > # Example 1
# > # The preferred way to use confirm
# > confirm Delete file1? && echo rm file1
# >
# > # Example 2
# > # Use the $? variable to examine confirm's return value
# > confirm Delete file2?
# > if [ $? -eq 0 ]
# > then
# > echo Another file deleted
# > fi
# >
# > # Example 3
# > # Tell bash to exit right away if any command returns a non-zero code
# > set -o errexit
# > confirm Do you want to run the rest of the script?
# > echo Here is the rest of the script
#
# ======================================================================
function confirm()
{
echo -n "$@ "
read -e answer
for response in y Y yes YES Yes Sure sure SURE OK ok Ok
do
if [ "_$answer" == "_$response" ]
then
return 0
fi
done
# Any answer other than the list above is considerred a "no" answer
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment