Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jokull
Created April 1, 2023 16:21
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 jokull/543283f9606f88d1ff46d83062e3526d to your computer and use it in GitHub Desktop.
Save jokull/543283f9606f88d1ff46d83062e3526d to your computer and use it in GitHub Desktop.
Push current tip to remote `develop` branch
#!/bin/bash
# Function to check if the branch exists on the remote
branch_exists_on_remote() {
git ls-remote --quiet --exit-code origin "refs/heads/$1" &>/dev/null
return $?
}
# Function to check if the branch exists locally
branch_exists_locally() {
git show-ref --quiet --verify "refs/heads/$1"
return $?
}
# Check if there are any changes to stash
if git diff-index --quiet HEAD --; then
echo "No changes to stash."
stash_required=false
else
# Stash changes
echo "Stashing changes..."
git stash
stash_required=true
fi
# Get current branch name
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Check if develop branch exists locally
if branch_exists_locally develop; then
# Prompt user for confirmation to delete local develop branch
echo "Local 'develop' branch exists."
read -r -p "Are you sure you want to delete the local 'develop' branch and create a new one? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
# Delete local develop branch
echo "Deleting local 'develop' branch..."
git branch -D develop
else
echo "Aborting operation."
exit 1
fi
fi
# Check if develop branch exists on remote
if branch_exists_on_remote develop; then
# Prompt user for confirmation to delete remote develop branch
echo "Remote 'develop' branch exists."
read -r -p "Are you sure you want to delete the remote 'develop' branch? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
# Delete remote develop branch at origin
echo "Deleting remote 'develop' branch..."
git push origin --delete develop
else
echo "Aborting operation."
exit 1
fi
fi
# Create new develop branch from current branch and push to origin
echo "Creating new 'develop' branch from current branch and pushing to origin..."
git checkout -b develop
git push origin develop
# Switch back to the original branch
echo "Switching back to the original branch..."
git checkout "$current_branch"
# Apply stashed changes if required
if [ "$stash_required" = true ]; then
echo "Applying stashed changes..."
git stash apply
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment