Script to run `git pull` inside all subdirectories which are git repositories. For Windows, macOS and Linux.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Script to run `git pull` inside all subdirectories which are git repositories. | |
:: usage 1: keep local changes, then up to date. | |
:: > git-pull.bat | |
:: usage 2: abort local changes, reset if possible, then up to date. | |
:: > git-pull.bat --force --no-stash | |
:: usage 3: try keep local changes, reset if possible, then up to date. | |
:: > git-pull.bat --force | |
@echo off | |
setlocal | |
:parse | |
IF "%~1"=="" GOTO endparse | |
IF "%~1"=="--force" set _FORCE=true | |
IF "%~1"=="--no-stash" set _NO_STASH=true | |
SHIFT | |
GOTO parse | |
:endparse | |
for /f "tokens=* delims=" %%i in ('dir /ad /b "."') do ( | |
echo --- Working on %cd%\%%i | |
pushd "%cd%\%%i" | |
if NOT EXIST ".git\" ( | |
echo x: \"%cd%\%%i\" is not a git repository, continue next directory. | |
) else ( | |
if not "%_NO_STASH%"=="true" ( git stash --quiet ) | |
if "%_FORCE%"=="true" ( git reset --hard ) | |
git pull | |
if not "%_NO_STASH%"=="true" ( git stash apply --quiet ) | |
) | |
popd | |
) | |
endlocal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script to run `git pull` inside all subdirectories which are git repositories. | |
# usage 1: keep local changes, then up to date. | |
# PS> ./git-pull.ps1 | |
# usage 2: abort local changes, reset if possible, then up to date. | |
# PS> ./git-pull.ps1 -Force -NoStash | |
# usage 3: try keep local changes, reset if possible, then up to date. | |
# PS> ./git-pull.ps1 -Force | |
param( | |
[Switch]$Force, | |
[Switch]$NoStash | |
) | |
foreach ($dir in Get-ChildItem -Directory) { | |
try { | |
pushd $dir.FullName | |
echo "--- Working on ""$($dir.FullName)""" | |
if ((Get-ChildItem -Force -Directory).Name -notcontains ".git") { | |
echo " x: ""$($dir.FullName)"" is not a git repository, continue next directory." | |
continue | |
} | |
if (!$NoStash) { git stash --quiet } | |
if ($Force) { git reset --hard } | |
git pull | |
if (!$NoStash) { git stash apply --quiet } | |
} finally { | |
popd | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o pipefail | |
# Script to run `git pull` inside all subdirectories which are git repositories. | |
# usage 1: keep local changes, then up to date. | |
# $ bash ./git-pull.sh | |
# usage 2: abort local changes, reset if possible, then up to date. | |
# $ bash ./git-pull.sh --force --no-stash | |
# usage 3: try keep local changes, reset if possible, then up to date. | |
# $ bash ./git-pull.sh --force | |
while [ $# -gt 0 ]; do | |
case $1 in | |
--force) _FORCE=true; shift 1; ;; | |
--no-stash) _NO_STASH=true; shift 1; ;; | |
*) shift ;; | |
esac | |
done | |
## find is not a good way to control. Additionally, this include CURRENT directory and it is unexpected. | |
# find . -maxdepth 1 -type d -exec bash -c 'echo "Working on $(realpath $1)"; git reset --force; git pull' shell {} \; | |
for dir in ./*/; do | |
echo "--- Working on \"$(realpath "${dir}")\"" | |
pushd "$(realpath "${dir}")" > /dev/null | |
if [[ ! -d ".git" ]]; then | |
echo " x: \"$(realpath "${dir}")\" is not a git repository, continue next directory." | |
popd > /dev/null | |
continue | |
fi | |
if [[ "${_NO_STASH:=false}" != "true" ]]; then git stash --quiet; fi | |
if [[ "${_FORCE:=false}" == "true" ]]; then git reset --hard; fi | |
git pull | |
if [[ "${_NO_STASH:=false}" != "true" ]]; then git stash apply --quiet; fi | |
popd > /dev/null | |
done |
added --no-stash
option.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
similar one: https://gist.github.com/phette23/7620214