|
#!/bin/bash |
|
|
|
# Git Sync Fork Script |
|
# Syncs a forked repository with its upstream |
|
|
|
set -e # Exit on any error |
|
|
|
# Colors for output |
|
RED='\033[0;31m' |
|
GREEN='\033[0;32m' |
|
YELLOW='\033[1;33m' |
|
BLUE='\033[0;34m' |
|
NC='\033[0m' # No Color |
|
|
|
# Function to print colored output |
|
print_error() { |
|
echo -e "${RED}Error: $1${NC}" >&2 |
|
} |
|
|
|
print_success() { |
|
echo -e "${GREEN}$1${NC}" |
|
} |
|
|
|
print_warning() { |
|
echo -e "${YELLOW}$1${NC}" |
|
} |
|
|
|
print_info() { |
|
echo -e "${BLUE}$1${NC}" |
|
} |
|
|
|
# Check if this is a Git repository |
|
if ! git rev-parse --git-dir > /dev/null 2>&1; then |
|
print_error "This is not a Git repository!" |
|
exit 1 |
|
fi |
|
|
|
print_success "✓ Git repository detected" |
|
|
|
# Check if upstream remote exists |
|
if ! git remote get-url upstream > /dev/null 2>&1; then |
|
print_error "No upstream remote found!" |
|
print_warning "Please add an upstream remote first:" |
|
print_info " git remote add upstream <upstream-repo-url>" |
|
exit 1 |
|
fi |
|
|
|
print_success "✓ Upstream remote found" |
|
|
|
# Get current branch name |
|
current_branch=$(git branch --show-current) |
|
print_info "Current branch: $current_branch" |
|
|
|
# Fetch from upstream |
|
print_info "Fetching from upstream..." |
|
git fetch upstream |
|
|
|
# Checkout main branch |
|
print_info "Checking out main branch..." |
|
if git show-ref --verify --quiet refs/heads/main; then |
|
git checkout main |
|
elif git show-ref --verify --quiet refs/heads/master; then |
|
print_warning "main branch not found, using master instead" |
|
git checkout master |
|
else |
|
print_error "Neither 'main' nor 'master' branch found!" |
|
exit 1 |
|
fi |
|
|
|
# Merge upstream into current branch |
|
print_info "Merging upstream into current branch..." |
|
git merge upstream/main 2>/dev/null || git merge upstream/master 2>/dev/null || { |
|
print_error "Failed to merge upstream. Please resolve conflicts manually." |
|
exit 1 |
|
} |
|
|
|
print_success "✓ Successfully merged upstream changes" |
|
|
|
# Ask if user wants to push to origin |
|
echo |
|
read -p "Do you want to push the changes to origin? (y/N): " -n 1 -r |
|
echo |
|
if [[ $REPLY =~ ^[Yy]$ ]]; then |
|
print_info "Pushing to origin..." |
|
git push origin |
|
print_success "✓ Changes pushed to origin" |
|
else |
|
print_info "Skipping push to origin" |
|
fi |
|
|
|
print_success "Git sync completed successfully!" |