Last active
September 1, 2024 15:57
-
-
Save progmult/bf9b6d8a4eea497dc75906d484636504 to your computer and use it in GitHub Desktop.
Batch script to automate merging many Git branches to master
This file contains hidden or 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
@echo off | |
chcp 65001 > nul | |
setlocal enabledelayedexpansion | |
:: Fetching the latest changes from the remote repository | |
git fetch --all | |
:: Switching to the master branch and getting the latest changes | |
git switch master | |
git reset --hard origin/master | |
git checkout . | |
git pull origin master | |
:: Interactive part for merging feature branches into the release branch | |
:askBranch | |
set "featureBranch=" | |
set /p featureBranch="Enter the name of the feature branch to merge (leave empty to finish): " | |
if "%featureBranch%"=="" goto endMerge | |
set "isNumeric=true" | |
for /f "delims=0123456789" %%i in ("%featureBranch%") do set "isNumeric=false" | |
if "%isNumeric%"=="true" ( | |
set featureBranch=task-%featureBranch% | |
) | |
:: Checking that the entered branch name is not an empty string | |
if "%featureBranch%"=="" ( | |
echo The branch name cannot be empty. Please try again. | |
goto askBranch | |
) | |
:: Checking if the branch exists in the remote repository | |
git rev-parse --verify --quiet origin/%featureBranch% | |
if errorlevel 1 ( | |
echo The branch %featureBranch% does not exist in the remote repository. Please try again. | |
goto askBranch | |
) | |
:: Switching to the feature branch, fetching the latest changes, and merging into the release branch | |
git switch %featureBranch% | |
git pull origin %featureBranch% | |
git switch master | |
git merge --no-ff %featureBranch% | |
:: Returning to the branch input prompt | |
goto askBranch | |
:endMerge | |
echo All specified branches have been merged into master | |
:: git push origin master | |
echo To push to the remote repository, run git push origin master (command copied to clipboard) | |
echo git push origin master | clip | |
pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment