Skip to content

Instantly share code, notes, and snippets.

@octylFractal
Last active February 8, 2019 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octylFractal/d70b299803796fa9f4dca5c9cf9df9fb to your computer and use it in GitHub Desktop.
Save octylFractal/d70b299803796fa9f4dca5c9cf9df9fb to your computer and use it in GitHub Desktop.
git psh (push -u -> psh): Push and set upstream if needed
#!/usr/bin/env bash
red="$(tput setaf 1)"
green="$(tput setaf 2)"
cyan="$(tput setaf 6)"
reset="$(tput init)"
branch="$(git rev-parse --abbrev-ref HEAD)"
[[ $? == 0 ]] || { echo "$red"'git `psh` cannot work without a branch'"$reset"; exit 1;}
upstream="$(git rev-parse --abbrev-ref "$branch"'@{u}' 2>/dev/null)"
if [[ $? == 0 ]]; then
echo "${green}Pushing to $upstream...${reset}"
git push
else
echo "${cyan}Pushing to origin/$branch and setting as upstream...${reset}"
git push -u origin "$branch"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment