Skip to content

Instantly share code, notes, and snippets.

@pda
Last active November 30, 2020 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pda/11336061 to your computer and use it in GitHub Desktop.
Save pda/11336061 to your computer and use it in GitHub Desktop.
git-push-as: pull request without creating branches.
#!/bin/bash -e
# Push local changes on the current branch as a new branch, then
# reset the local branch back to its remote tracking branch.
#
# This enables a fast workflow:
# 10 `git commit` one or more changes to main.
# 20 `git push-as -p new-feature`
# 30 goto 10
while getopts "p" opt; do
case "$opt" in
"p") PULL_REQUEST="pr";;
esac
done
shift $(( OPTIND - 1 ))
main() {
if ! git diff-index --quiet HEAD; then
echo "Politely refusing to destroy uncommitted changes." >&2
exit 1
fi
local branch_new="$1"
if [ -z "$branch_new" ]; then
echo "Usage: $0 <new-branch>" >&2
exit 1
fi
local branch_base=$(git symbolic-ref --short HEAD)
local remote=$(git remote | head -n1 || echo "origin")
echo "Creating new local branch: $branch_new"
git branch "$branch_new"
echo "Resetting local $branch_base to $remote/$branch_base"
git reset --hard "$remote/$branch_base"
echo "Pushing $branch_new to $remote"
git push --set-upstream "$remote" "$branch_new"
if [ -n "$PULL_REQUEST" ]; then
# `hub pull-request -h $branch_new` should work, but...
git checkout "$branch_new"
hub pull-request
git checkout "$branch_base"
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment