Files
scripts/gpull
2025-12-18 10:00:05 +08:00

55 lines
1.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ---------------------------------------------------------------------------
# gpull Pull changes from a remote Git repository
#
# Usage:
# ./gpull [repo] [remote] [branch]
#
# repo (optional) Directory of repository to pull, default is current directory.
# remote (optional) Remote name, default is origin.
# branch (optional) Branch name, default is current branch.
#
# The script will pull the specific remote branch into the local repository.
# ---------------------------------------------------------------------------
# Get command line arguments
repo_dir=${1:-$(pwd)}
remote=${2:-origin}
branch=${3:-}
# Change to the specified directory
if [ ! -d "$repo_dir" ]; then
echo "Error: Directory '$repo_dir' does not exist."
exit 1
fi
cd "$repo_dir" || exit 1
# Check if the current directory is a git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Error: '$repo_dir' is not a git repository."
exit 1
fi
# If no branch specified, get the current branch
if [ -z "$branch" ]; then
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" = "HEAD" ]; then
echo "Error: Not currently on a branch. Please specify a branch to pull."
exit 1
fi
fi
echo "Pulling changes from $remote/$branch into $repo_dir"
# Perform the git pull
git pull "$remote" "$branch"
# Check if the pull was successful
if [ $? -eq 0 ]; then
echo "Successfully pulled changes from $remote/$branch"
else
echo "Error: Failed to pull changes from $remote/$branch"
exit 1
fi