#!/bin/bash # Check if a branch name was provided if [ -z "$1" ]; then echo "Error: Please provide the name of the branch to merge" echo "Usage: ./git-merge.sh branch_name" exit 1 fi BRANCH_NAME=$1 # Check if the branch exists if ! git show-ref --verify --quiet refs/heads/$BRANCH_NAME; then echo "Error: Branch '$BRANCH_NAME' does not exist" exit 1 fi # Display the steps that will be executed echo "=== Starting merge process ===" echo "1. Checkout to main" echo "2. Pull latest changes" echo "3. Merge branch $BRANCH_NAME" echo "4. Push to origin" echo "5. Delete local and remote branches" # Ask for confirmation read -p "Do you want to continue? (y/n) " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Operation cancelled" exit 1 fi # Execute commands echo -e "\n=== Checking out to main ===" git checkout main if [ $? -ne 0 ]; then echo "Error during checkout to main" exit 1 fi echo -e "\n=== Pulling latest changes ===" git pull origin main if [ $? -ne 0 ]; then echo "Error during pull" exit 1 fi echo -e "\n=== Merging branch $BRANCH_NAME ===" git merge $BRANCH_NAME if [ $? -ne 0 ]; then echo "Error during merge. Please resolve conflicts manually" exit 1 fi echo -e "\n=== Pushing to origin ===" git push origin main if [ $? -ne 0 ]; then echo "Error during push" exit 1 fi echo -e "\n=== Deleting local branch ===" git branch -d $BRANCH_NAME if [ $? -ne 0 ]; then echo "Warning: Unable to delete local branch" echo "If you are sure everything is properly merged, use: git branch -D $BRANCH_NAME" fi echo -e "\n=== Deleting remote branch ===" git push origin --delete $BRANCH_NAME if [ $? -ne 0 ]; then echo "Warning: Unable to delete remote branch" fi echo -e "\n=== Merge process completed successfully ==="