Skip to content

Instantly share code, notes, and snippets.

@nikalas
Last active February 13, 2024 06:50
Show Gist options
  • Save nikalas/8cfa928836e757d63bb291a45fb17d6c to your computer and use it in GitHub Desktop.
Save nikalas/8cfa928836e757d63bb291a45fb17d6c to your computer and use it in GitHub Desktop.
will merge current branch into release/dev and switch back to current branch
#!/usr/bin/env bash
# This script will merge the current branch into the provided branch or release/dev branch
# and then switch back to the current branch.
# Save as quick-merge in your path (ex. ~/bin/quick-merge) and make executable, then source it from `.bashrc` or `.zshrc`
# by adding the folder to your path `export PATH="$HOME/bin:$PATH"`
# The script can be used as a base with aliases for other branches eg. `alias mprod='quick-merge release/prod'`
set -e
# Check if a command line argument is provided
if [ -z "$1" ]
then
MERGE_TARGET="release/dev"
else
MERGE_TARGET=$1
fi
# ask for confirmation
read -p "Are you sure you want to merge into $MERGE_TARGET? (y/n) " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo
exit 1
fi
# set up colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
CURRENT_BRANCH=$( git rev-parse --abbrev-ref HEAD )
echo $CURRENT_BRANCH
git checkout $MERGE_TARGET
git pull
git merge $CURRENT_BRANCH
if [ $? -ne 0 ]; then
printf " :: ${RED}Error:${NC} Merge failed. Exiting \n"
exit 1
else
printf " :: ${GREEN}Merge successful${NC} \n"
git push
fi
git checkout $CURRENT_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment