Added gpull.

This commit is contained in:
Ali
2025-12-18 10:00:05 +08:00
parent 60589218dc
commit da2260d414

54
gpull Executable file
View File

@@ -0,0 +1,54 @@
#!/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