Created
May 17, 2017 01:02
-
-
Save jimsynz/7051cfaee1b4f5669eeee0440f3dfada to your computer and use it in GitHub Desktop.
Try and guess where to push branches to based on convention.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script tries to handle `git push` when the local branch isn't | |
# configured to track a remote branch. | |
# | |
# First, if the branch already tracks a remote branch then it just calls | |
# `git push` and we're done. | |
# | |
# Second, if the branch doesn't track a remote branch and there is a | |
# remote called "origin" present then it just makes an assumption and | |
# pushes it there. | |
# | |
# Lastly, it gives up and spits out a nice error message telling you | |
# what you need to do. | |
FULL_REF_NAME=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)) | |
CURRENT_BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
if [[ -n $FULL_REF_NAME ]]; then | |
# We're already tracking a remote branch, so just push it. | |
exec git push $@ | |
else | |
# Check to see if there is an "origin" remote. | |
if [[ $(git remote | grep -c '^origin$') == "1" ]]; then | |
echo "Warning: this branch doesn't track origin. Pushing there." | |
exec git push --set-upstream origin $CURRENT_BRANCH_NAME $@ | |
else | |
echo "Warning: this branch doesn't track a remote branch" | |
echo " additionally there is no 'origin' remote." | |
if [[ $(git remote | wc -l) -gt 0 ]]; then | |
echo | |
echo "Currently configured remotes are:" | |
git remote -v | |
else | |
echo " you'll need to add one manually." | |
fi | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment