Update sync_with_git.sh
This commit is contained in:
@@ -3,24 +3,16 @@
|
|||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# Script Description:
|
# Script Description:
|
||||||
# This script connects an existing local directory to a remote Git repository.
|
# This script connects an existing local directory to a remote Git repository.
|
||||||
# It requires the SSH URL of the Git repository as an input argument.
|
|
||||||
#
|
#
|
||||||
# Process:
|
# Process:
|
||||||
# 1. Initializes a new local Git repository.
|
# 1. Initializes a new local Git repository.
|
||||||
# 2. Adds the provided URL as the remote 'origin'.
|
# 2. Adds the remote 'origin'.
|
||||||
# 3. Fetches the remote repository data.
|
# 3. Fetches the remote repository data.
|
||||||
# 4. Checks for differences between the local files and 'origin/master'.
|
# 4. Stages local files to check differences.
|
||||||
# 5. If there are no differences, it sets up tracking for the master branch
|
# 5. Identifies real changes bypassing the Git --name-only whitespace bug.
|
||||||
# and performs a hard reset to synchronize exactly with the remote.
|
# 6. Prompts to override local files if diffs are found.
|
||||||
#
|
|
||||||
# Usage:
|
|
||||||
# ./connect_git.sh <repository-ssh-url>
|
|
||||||
#
|
|
||||||
# Example:
|
|
||||||
# ./connect_git.sh git@github.com:username/repository.git
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
|
||||||
# Check if the repository URL argument is provided
|
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
echo "Error: No repository SSH URL provided."
|
echo "Error: No repository SSH URL provided."
|
||||||
echo "Usage: $0 <repository-ssh-url>"
|
echo "Usage: $0 <repository-ssh-url>"
|
||||||
@@ -39,24 +31,46 @@ git remote add origin "$REPO_URL"
|
|||||||
echo "Fetching from origin..."
|
echo "Fetching from origin..."
|
||||||
git fetch origin
|
git fetch origin
|
||||||
|
|
||||||
# Check differences between the local working directory and the remote branch.
|
echo "Staging local files so Git can compare them..."
|
||||||
# Using 'git diff --quiet origin/master' safely checks without needing HEAD to exist.
|
git add .
|
||||||
echo "Checking differences..."
|
|
||||||
if git diff --quiet "origin/$BRANCH"; then
|
|
||||||
echo "No differences found between local folder and remote origin/$BRANCH."
|
|
||||||
|
|
||||||
echo "Setting up tracking and aligning with remote..."
|
echo "Checking for differences (ignoring line-endings and whitespace)..."
|
||||||
# Ensure we are on the master branch
|
if git diff --cached --quiet --ignore-cr-at-eol --ignore-space-at-eol "origin/$BRANCH"; then
|
||||||
git checkout -B "$BRANCH"
|
echo "Local folder matches remote origin/$BRANCH exactly."
|
||||||
# Set the upstream tracking branch
|
|
||||||
git branch --set-upstream-to="origin/$BRANCH" "$BRANCH"
|
|
||||||
# Hard reset to match the remote exactly
|
|
||||||
git reset --hard "origin/$BRANCH"
|
|
||||||
|
|
||||||
echo "Success! The local folder is now completely linked to the remote repository."
|
|
||||||
else
|
else
|
||||||
echo "Notice: Differences exist between the local files and origin/$BRANCH."
|
echo ""
|
||||||
echo "Tracking was not set automatically. You can review differences using:"
|
echo "Differences exist in the following files:"
|
||||||
echo " git diff origin/$BRANCH"
|
|
||||||
|
# We use --numstat instead of --name-only.
|
||||||
|
# Files with only whitespace diffs will output '0 \t 0 \t filename'.
|
||||||
|
# Awk filters out the ones where added ($1) and deleted ($2) are both 0.
|
||||||
|
# Note: Binary changes show as '-' rather than '0', which awk will correctly keep.
|
||||||
|
git diff --cached --numstat --ignore-cr-at-eol --ignore-space-at-eol "origin/$BRANCH" | \
|
||||||
|
awk -F'\t' '$1!="0" || $2!="0" {print $3}'
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
read -p "Do you want to discard your local changes and accept the remote version for these files? (y/N): " choice
|
||||||
|
case "$choice" in
|
||||||
|
[yY]* )
|
||||||
|
echo "Accepting remote versions..."
|
||||||
|
;;
|
||||||
|
* )
|
||||||
|
echo "Operation aborted by user."
|
||||||
|
echo "You can review full diffs manually by running: git diff --cached --ignore-cr-at-eol --ignore-space-at-eol origin/$BRANCH"
|
||||||
exit 1
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Setting up tracking and aligning with remote..."
|
||||||
|
|
||||||
|
# 1. First ensure we are on the correct branch name
|
||||||
|
git checkout -B "$BRANCH"
|
||||||
|
|
||||||
|
# 2. Reset hard FIRST so the branch actually has commits
|
||||||
|
git reset --hard "origin/$BRANCH"
|
||||||
|
|
||||||
|
# 3. NOW set the upstream tracking, since the branch is no longer empty
|
||||||
|
git branch --set-upstream-to="origin/$BRANCH" "$BRANCH"
|
||||||
|
|
||||||
|
echo "Success! The local folder is now completely linked to the remote repository."
|
||||||
|
|||||||
Reference in New Issue
Block a user