Last active
August 17, 2022 09:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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