Skip to content

Instantly share code, notes, and snippets.

@ierceg
Last active January 17, 2024 18:39
Show Gist options
  • Save ierceg/5d6afb898af4d628cb91e6683490b8e3 to your computer and use it in GitHub Desktop.
Save ierceg/5d6afb898af4d628cb91e6683490b8e3 to your computer and use it in GitHub Desktop.
Checkout master, pull, checkout previous current branch, merge master into it.
#!/bin/bash
# Save the current branch name
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ $? -ne 0 ]; then
echo "Error: Failed to get the current branch name."
exit 1
fi
# Set target branch to 'master' or the provided argument
target_branch=${1:-master}
# Checkout the target branch
git checkout "$target_branch"
if [ $? -ne 0 ]; then
echo "Error: Failed to checkout $target_branch."
exit 1
fi
# Pull the latest changes
git pull
if [ $? -ne 0 ]; then
echo "Error: Failed to pull the latest changes on $target_branch."
git checkout "$current_branch"
exit 1
fi
# Checkout the original branch
git checkout "$current_branch"
if [ $? -ne 0 ]; then
echo "Error: Failed to checkout $current_branch."
exit 1
fi
# Merge the target branch into the current branch
git merge "$target_branch" --no-edit
if [ $? -ne 0 ]; then
echo "Error: Failed to merge $target_branch into $current_branch."
exit 1
fi
echo "Merge of $target_branch into $current_branch completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment