3

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
2
  • 1
    you need a delayed expansion Commented Apr 30, 2020 at 13:30
  • Or do not assign %%i to a variable but use it directly (git checkout %%i)... Commented Apr 30, 2020 at 19:19

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.