Last active
July 11, 2024 07:07
-
-
Save Vyom-Yadav/f18c7163ee053e79df657c953b8e27a2 to your computer and use it in GitHub Desktop.
Update Git Repos
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 -euo pipefail | |
GIT_DIR="$HOME/git-pulls" | |
update_repo() { | |
echo "Updating $1..." | |
cd "$1" | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
default_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') | |
if [ "$current_branch" != "$default_branch" ]; then | |
echo "Warning: Not on default branch ($default_branch) in $1. Skipping." | |
return 0 | |
fi | |
if git remote | grep -q "^upstream$"; then | |
echo "Updating from upstream in $1..." | |
git fetch upstream "$default_branch" | |
git merge --ff-only "upstream/$default_branch" | |
else | |
echo "No upstream found. Updating from origin in $1..." | |
git fetch origin "$default_branch" | |
git merge --ff-only "origin/$default_branch" | |
fi | |
echo "Successfully updated $1" | |
} | |
if [ ! -d "$GIT_DIR" ]; then | |
echo "Error: Directory $GIT_DIR does not exist." >&2 | |
exit 1 | |
fi | |
for repo in "$GIT_DIR"/*; do | |
if [ -d "$repo/.git" ]; then | |
update_repo "$repo" | |
fi | |
done | |
echo "All repositories processed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment