Last active
December 4, 2022 17:26
-
-
Save meros/67937605c310c806f48b476ec5546abf to your computer and use it in GitHub Desktop.
Set upstream to branch, pull with --rebase. Also stash/stash pop before/after.
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 | |
# Set the `set -e` and `set -o pipefail` options to exit gracefully if any | |
# command fails and to propagate the exit status of a command in a pipe | |
set -e | |
set -o pipefail | |
# Set the remote repository name (default: origin) | |
REMOTE_NAME=${1:-origin} | |
# Get the current branch name | |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
# Check if the upstream branch exists | |
if ! git show-ref --verify --quiet refs/remotes/$REMOTE_NAME/$CURRENT_BRANCH; then | |
# Print an error message and exit | |
echo "❌ The upstream branch '$REMOTE_NAME/$CURRENT_BRANCH' does not exist." | |
echo "Exiting script." | |
exit 1 | |
fi | |
# Stash any local changes | |
echo "💾 Stashing local changes..." | |
git stash > /dev/null | |
# Set the upstream tracking branch for the current branch | |
echo "📡 Setting up tracking branch for $CURRENT_BRANCH..." | |
git branch --set-upstream-to=$REMOTE_NAME/$CURRENT_BRANCH $CURRENT_BRANCH > /dev/null | |
# Pull the remote changes with a rebase | |
echo "📥 Pulling remote changes for $CURRENT_BRANCH..." | |
git pull --rebase > /dev/null | |
# Stash pop the local changes | |
echo "💾 Restoring stashed local changes..." | |
git stash pop > /dev/null | |
# Print a success message | |
echo "✅ Done! Your local branch '$CURRENT_BRANCH' is now up to date with '$REMOTE_NAME/$CURRENT_BRANCH'." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment