Skip to content

Instantly share code, notes, and snippets.

@ShiftSad
Last active January 2, 2025 02:31
Show Gist options
  • Save ShiftSad/23d557908d22a2a3600c80d12cbf16a4 to your computer and use it in GitHub Desktop.
Save ShiftSad/23d557908d22a2a3600c80d12cbf16a4 to your computer and use it in GitHub Desktop.
Script to allow for git syncronization
#!/bin/bash
# Function to print messages
log() {
echo "$(date +"%Y-%m-%d %H:%M:%S") - $1"
}
# Check arguments
if [ "$#" -ne 2 ]; then
log "Usage: $0 <remote> <branch>"
exit 1
fi
REMOTE=$1
BRANCH=$2
# Ensure Git is initialized
if [ ! -d .git ]; then
log "No Git repository found. Initializing repository..."
git init
fi
# Add the remote if it doesn't exist
if ! git remote | grep -q origin; then
log "Adding remote 'origin'..."
git remote add origin $REMOTE
fi
# Fetch and check out the branch
log "Fetching and checking out branch '$BRANCH'..."
git fetch origin $BRANCH
git checkout -B $BRANCH origin/$BRANCH 2>/dev/null || {
log "Branch '$BRANCH' not found on remote. Creating and tracking it locally..."
git checkout -b $BRANCH
}
# Force-align the local branch with the remote branch
log "Pulling and aligning local repository with remote '$REMOTE', branch '$BRANCH'..."
git reset --hard origin/$BRANCH
# Clean untracked files
log "Cleaning untracked files..."
git clean -fd
log "Repository is now up-to-date with the remote."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment