Created
September 27, 2021 21:18
-
-
Save gene1wood/ddd66d79fc472f351a04c643aecbcf75 to your computer and use it in GitHub Desktop.
Update a local git repo by switching to master, fetching all remotes, deleting all local branches that track a remote branch that's now gone, fast forward merge upstream into local master and push local master to origin
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 -e | |
# https://gist.github.com/gene1wood/ddd66d79fc472f351a04c643aecbcf75 | |
log() { | |
echo -e "\e[92m${1}\e[0m" | |
} | |
if ! git status >/dev/null 2>&1; then | |
log "Must be in a git repo" | |
exit 1 | |
fi | |
UPSTREAM_REMOTE=upstream | |
ORIGIN_REMOTE=origin | |
DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') | |
log "Checking out $DEFAULT_BRANCH" | |
git checkout "$DEFAULT_BRANCH" | |
for remote in $(git remote); do | |
log "Fetching remote $remote" | |
git fetch --progress --prune --recurse-submodules=no "$remote" | |
done | |
log "Deleting any local branches which are tracking remote branches that no longer exist" | |
LANG=en git branch --format='%(if:equals=gone)%(upstream:track,nobracket)%(then)%(refname:short)%(end)' | grep '.' | xargs --no-run-if-empty git branch -D | |
if git remote get-url $UPSTREAM_REMOTE >/dev/null 2>&1; then | |
log "Fast forward merging $UPSTREAM_REMOTE into our local ${DEFAULT_BRANCH}" | |
git merge --no-commit --ff ${UPSTREAM_REMOTE}/${DEFAULT_BRANCH} | |
else | |
log "There is no ${UPSTREAM_REMOTE} remote. Skipping fast forward merge of ${DEFAULT_BRANCH}" | |
fi | |
if git remote get-url $ORIGIN_REMOTE >/dev/null 2>&1; then | |
log "Pushing ${DEFAULT_BRANCH} to $ORIGIN_REMOTE" | |
git push --porcelain --progress $ORIGIN_REMOTE refs/heads/${DEFAULT_BRANCH}:refs/heads/${DEFAULT_BRANCH} | |
else | |
log "There is no $ORIGIN_REMOTE remote. Skipping push of ${DEFAULT_BRANCH}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment