Skip to content

Instantly share code, notes, and snippets.

@tueda
Last active July 27, 2024 12:09
Show Gist options
  • Save tueda/726158ffc5de9c093609c1c1d82b1f07 to your computer and use it in GitHub Desktop.
Save tueda/726158ffc5de9c093609c1c1d82b1f07 to your computer and use it in GitHub Desktop.
Push the current Git branch to the remote's "develop" branch anyway. #bin #bash
#!/bin/bash
#
# @file git-push-as-develop
#
# Pushes the current Git branch to the remote "develop" branch.
#
# Examples:
# push-as-develop.sh # to "develop" in "origin"
# push-as-develop.sh upstream # to "develop" in "upstream"
# push-as-develop.sh upstream new-branch # to "new-branch" in "upstream"
#
set -eu
set -o pipefail
target_repository=origin
target_branch=develop
if [[ $# -eq 0 ]]; then
:
elif [[ $# -eq 1 ]]; then
target_repository=$1
elif [[ $# -eq 2 ]]; then
target_repository=$1
target_branch=$2
else
echo "error: too many arguments ($#)" >&2
exit 1
fi
current_branch=$(git symbolic-ref --short HEAD)
workdir_is_dirty=$(if [[ $(git diff --stat) != '' ]]; then echo :; else echo false; fi)
if $workdir_is_dirty; then
echo 'error: working directory is dirty' >&2
exit 1
fi
target_branch_exists=$(if [[ $(git branch --list ${target_branch}) != '' ]]; then echo :; else echo false; fi)
if $target_branch_exists; then
git switch $target_branch
else
git switch -c $target_branch
fi
trap "git switch $current_branch" EXIT
git reset --hard $current_branch
git push --force $target_repository $target_branch
echo '==============================================================================='
git reflog $target_branch | head -10
echo '==============================================================================='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment