首页 > 解决方案 > Batch file : git checkout branch using variables

问题描述

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

标签: gitbatch-file

解决方案


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


推荐阅读