Skip to content

Instantly share code, notes, and snippets.

@aessing
Last active January 27, 2026 21:53
Show Gist options
  • Select an option

  • Save aessing/8f494b882277d17dacdd0525da710b15 to your computer and use it in GitHub Desktop.

Select an option

Save aessing/8f494b882277d17dacdd0525da710b15 to your computer and use it in GitHub Desktop.
Script that updates all GIT repositories in subdirectories of the actual folder.
#!/usr/bin/env bash
# =============================================================================
# Helper Script: git-sync-all.sh
# Updates all git repositories in subdirectories
# -----------------------------------------------------------------------------
# Developer.......: Andre Essing (https://github.com/aessing)
# (https://www.linkedin.com/in/aessing/)
# -----------------------------------------------------------------------------
# THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# =============================================================================
# Color codes
BOLD='\033[1m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
RESET='\033[0m'
# Store the current directory
ORIGINAL_DIR=$(pwd)
# Check for --debug flag
DEBUG=false
for arg in "$@"; do
if [[ "$arg" == "--debug" ]]; then
DEBUG=true
fi
done
echo ""
echo -e "${BOLD}${BLUE}═══════════════════════════════════════════════════════════${RESET}"
echo -e "${BOLD}${GREEN}🔄 GIT Sync - Repository Refresh Tool${RESET}"
echo -e "${BOLD}${BLUE}═══════════════════════════════════════════════════════════${RESET}"
echo ""
echo -e "${BOLD}Starting directory:${RESET} $ORIGINAL_DIR"
echo ""
# Menu to select sync mode
echo -e "${BOLD}Select sync mode:${RESET}"
echo -e " ${GREEN}1)${RESET} git fetch --all --prune (fetch only, no merge)"
echo -e " ${GREEN}2)${RESET} git pull --all --prune (fetch and merge)"
echo -e " ${GREEN}3)${RESET} git pull --all --prune --no-rebase (fetch and merge without rebase)"
echo ""
read -p "Enter your choice [1-2]: " choice
case $choice in
1)
GIT_CMD="git fetch --all --prune"
GIT_CMD_DESC="git fetch --all --prune"
;;
2)
GIT_CMD="git pull --all --prune"
GIT_CMD_DESC="git pull --all --prune"
;;
3)
GIT_CMD="git pull --all --prune --no-rebase"
GIT_CMD_DESC="git pull --all --prune --no-rebase"
;;
*)
echo -e "${YELLOW}Invalid choice. Defaulting to git fetch --all --prune${RESET}"
GIT_CMD="git fetch --all --prune"
GIT_CMD_DESC="git fetch --all --prune"
;;
esac
echo ""
echo -e "${BOLD}Selected mode:${RESET} $GIT_CMD_DESC"
echo ""
# Array to track skipped folders
SKIPPED_DIRS=()
# Function to process a directory
process_dir() {
local dir=$1
# Enter the directory
cd "$dir" || {
echo -e "${RED}✗ Error: Could not enter directory $dir${RESET}"
return 1
}
# Check if it's a git repository
if [ -d ".git" ]; then
echo -e "${BLUE}📁 $dir${RESET}"
echo -e "${GREEN}✓ Running $GIT_CMD_DESC...${RESET}"
$GIT_CMD
echo ""
else
SKIPPED_DIRS+=("$dir")
fi
# Go back to the original directory
cd "$ORIGINAL_DIR" || {
echo -e "${RED}✗ Error: Could not return to original directory${RESET}"
exit 1
}
}
# Loop through all top-level subdirectories
for dir in */; do
# Check if it's actually a directory
if [ -d "$dir" ]; then
process_dir "$dir"
# Loop through second-level subdirectories (1 level deep)
for subdir in "$dir"*/; do
# Check if it's actually a directory
if [ -d "$subdir" ]; then
process_dir "$subdir"
fi
done
fi
done
echo -e "${BOLD}${BLUE}═══════════════════════════════════════════════════════════${RESET}"
echo -e "${BOLD}${GREEN}✓ Finished processing all subdirectories${RESET}"
echo -e "${BOLD}${BLUE}═══════════════════════════════════════════════════════════${RESET}"
echo ""
# Show summary of skipped folders (only with --debug flag)
if [ "$DEBUG" = true ] && [ ${#SKIPPED_DIRS[@]} -gt 0 ]; then
echo -e "${BOLD}${YELLOW}Skipped folders (not git repositories):${RESET}"
for dir in "${SKIPPED_DIRS[@]}"; do
echo -e " ${YELLOW}⊘ $dir${RESET}"
done
echo ""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment