Skip to content

Instantly share code, notes, and snippets.

@mbenford
Last active August 17, 2022 09:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbenford/79d8771653dfbc1ce92a to your computer and use it in GitHub Desktop.
Save mbenford/79d8771653dfbc1ce92a to your computer and use it in GitHub Desktop.
Bash script to update a local copy of a Git repository with latest changes from a remote branch
#!/bin/bash
set -e
local_branch=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
remote_branch=$(git rev-parse --abbrev-ref --symbolic-full-name @{u})
remote=$(git config branch.$local_branch.remote)
echo "Fetching from $remote..."
git fetch $remote
if git merge-base --is-ancestor $remote_branch HEAD; then
echo 'Already up-to-date'
exit 0
fi
if git merge-base --is-ancestor HEAD $remote_branch; then
echo 'Fast-forward possible. Merging...'
git merge --ff-only --stat $remote_branch
else
echo 'Fast-forward not possible. Rebasing...'
git rebase --preserve-merges --stat $remote_branch
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment