Last active
September 13, 2016 21:30
-
-
Save mhutch/722fff2315597fc7db28e660423efb5c to your computer and use it in GitHub Desktop.
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 | |
# git-offload: a tool for shifting local commits to a new branch and pushing it | |
# Author: Mikayla Hutchinson <[email protected]> | |
if [ "$#" -ne 2 ]; then | |
echo | |
echo "Creates new branch from the current commit and pushes it to a remote" | |
echo "and then resets the original branch to its upstream state." | |
echo "" | |
echo "USAGE:" | |
echo " $(basename $0) remote new-branch" | |
echo | |
exit 1 | |
fi | |
NEWREMOTE=$1 | |
NEWBRANCH=$2 | |
NEWUPSTREAM="$NEWREMOTE/$NEWBRANCH" | |
BRANCH=$(git branch 2> /dev/null) | |
if [ "$?" -ne 0 ]; then | |
echo "not a git repository" | |
exit 1 | |
fi | |
BRANCH=$(echo "$BRANCH" | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
UPSTREAM=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)) | |
if [ -z $UPSTREAM ]; then | |
echo "branch $BRANCH has no upstream configured" | |
exit 1 | |
fi | |
CHANGED=$(git diff-index --name-only HEAD --) | |
if [ ! -z "$CHANGED" ]; then | |
echo "there are local changes, please commit" | |
exit 1 | |
fi | |
echo "$BRANCH $UPSTREAM -> $NEWBRANCH $NEWUPSTREAM" | |
git checkout -b $NEWBRANCH || exit 1 | |
git push -u $NEWREMOTE $NEWBRANCH || exit 1 | |
git checkout $BRANCH | |
git reset --hard $UPSTREAM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍