Skip to content

Instantly share code, notes, and snippets.

@Vyom-Yadav
Last active July 11, 2024 07:07
Show Gist options
  • Save Vyom-Yadav/f18c7163ee053e79df657c953b8e27a2 to your computer and use it in GitHub Desktop.
Save Vyom-Yadav/f18c7163ee053e79df657c953b8e27a2 to your computer and use it in GitHub Desktop.
Update Git Repos
#!/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