Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file : git checkout branch using variables

Tags:

git

batch-file

I have an issue to set up this batch. I want to get to my project directories, git fetch -all, switch branch to develop, pull, and go back to my actual branch For now I have succeded in doing so, but is's impossible to checkout a branch using variables I don't know why..

In short what I want to achieve is (on branch develop):

set current_branch=custom
git checkout %current_branch%

Here is the complete batch file

@echo off
SET project_array="c:\example\project01" "c:\example\project02"
for %%a in (%project_array%) do (
    echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo fetching datas for project at: %%a
    cd %%a
    @echo on
    for /F "tokens=*" %%i in ('git branch --show-current') do set current_branch=%%i
    git fetch --all
    git checkout develop
    git pull
    git checkout %current_branch%
    @echo off
)
PAUSE
like image 429
choutiru Avatar asked Apr 06 '26 04:04

choutiru


1 Answers

try this (not tested):

@echo off
SET "project_array="c:\example\project01" "c:\example\project02""
setlocal enableDelayedExpansion
for %%a in (%project_array%) do (
    echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    echo fetching datas for project at: %%a
    cd %%a
    @echo on
    for /F "tokens=*" %%i in ('git branch --show-current') do set current_branch=%%i
    git fetch --all
    git checkout develop
    git pull
    git checkout !current_branch!
    @echo off
)
PAUSE

More on Delayed Expansion

like image 102
npocmaka Avatar answered Apr 08 '26 19:04

npocmaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!