Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active May 10, 2023 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/2623623a0e1bc7fe86b5cf56e0c70d88 to your computer and use it in GitHub Desktop.
Save guitarrapc/2623623a0e1bc7fe86b5cf56e0c70d88 to your computer and use it in GitHub Desktop.
Script to run `git pull` inside all subdirectories which are git repositories. For Windows, macOS and Linux.
:: 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
# 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
}
}
#!/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
@guitarrapc
Copy link
Author

@guitarrapc
Copy link
Author

added --no-stash option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment