批处理文件:使用变量检出Git分支

3

我有一个设置批处理的问题。

我想进入我的项目目录,使用git fetch -all命令,切换到develop分支,执行pull操作,然后回到我的实际分支。

目前我已经成功地做到了这一点,但是使用变量检出分支是不可能的,我不知道为什么。

简而言之,我想要实现的是(在develop分支上):

set current_branch=custom
git checkout %current_branch%

这是完整的批处理文件

@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

1
你需要延迟扩展 - npocmaka
或者不将%%i分配给一个变量,而是直接使用它(git checkout %%i)... - aschipfl
1个回答

2

尝试这个(未经测试):

@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

关于延迟扩展的更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接