0
1

複数のGitリポジトリをまとめてPullするBat

Last updated at Posted at 2021-12-22

batファイルが置かれているフォルダは以下のすべての
Gitリポジトリに対して、stash → pull → stash pop を行う。

pull.bat
@echo off

rem batファイルが置いてある場所のすべてのフォルダパスを取得
for /d %%d in (%~dp0*) do (
  echo "start------------>"%%d
  cd %%d

  echo "stash------------>"
  git stash
  echo.

  echo "pull------------>"
  git pull
  echo.

  echo "pop------------>"
  git stash pop
  echo.

  echo "------------>end"%%d
)

pause

すべてのローカルブランチに対して行う。
(サブモジュールにも対応)

pull.bat
@echo off
setlocal enabledelayedexpansion

call:log "Start!"

REM フォルダ内のすべてのフォルダ名を取得
for /d %%i in (%~dp0*) do (
  REM 取得したフォルダ名のフォルダへ移動
  call:log "Processing folder: %%i"
  cd %%i
  cd

  REM 移動したフォルダがGitリポジトリの場合、Pullする
  if exist ".git" (
    call:gitPull
    for /f "delims=" %%s in ('git submodule status --recursive') do (
    REM サブモジュールのフォルダ名を取得
      for /f "tokens=2,3" %%c in ("%%s") do (
        call:log "Processing submodule: %%c"
        REM サブモジュールのフォルダへ移動
        cd %%c
        cd
        if exist ".git" (
          call:gitPull
        ) else (
          call:log "Not a Git repository. Skipping."
        )
      )
    )
  ) else (
    call:log "Not a Git repository. Skipping."
  )
)
call:log "End."

endlocal
pause

:gitPull
  REM 今ある変更をStashへ移動
  call:log "git stash"
  git stash

  REM 在のブランチ名を変数へ保存
  for /f "delims=" %%b in ('git branch --show-current') do set current_branch=%%b
  call:log "Current branch: !current_branch!"

  REM リポジトリ内のすべてのローカルブランチを取得
  for /f "delims=" %%a in ('git branch --format="%%(refname)"') do (
  
    REM 取得したローカルブランチへチェックアウト
    call:log "git checkout: %%~na"
    git checkout "%%~na"

    REM チェックアウトしたブランチに対してPull
    call:log "git pull"
    git pull
  )

  REM 変数へ保持したブランチへチェックアウト
  call:log "git checkout: !current_branch!"
  git checkout "!current_branch!"

  REM Stashした変更を適応
  call:log "stash pop"
  git stash pop
exit /b

:log
echo.
echo ------LOG------ %~1
echo.
exit /b

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1