Skip to content

Instantly share code, notes, and snippets.

@atika
Created December 23, 2014 20:11
Show Gist options
  • Save atika/c82e9410bbe7cd237c63 to your computer and use it in GitHub Desktop.
Save atika/c82e9410bbe7cd237c63 to your computer and use it in GitHub Desktop.
Update a GitHub Forked Repository with his Upstream/Master (and update remote master)
#!/bin/bash
# usage: git-sync-upstream "/your/git/folder/" [rebase/merge]
action="merge"
if [ $# -ge 1 ]; then cd "$1"; fi
if [ $# -eq 2 ]; then if [ "$2" = "rebase" ]; then action="rebase"; fi; fi
clear;
echo "Working on `pwd`"
if [ -d ".git" ]; then
current_branch="$(git status -b | head -n1 | sed 's/On branch //g')"
if git remote -v | grep -q upstream; then
git checkout master
[ $? -ne 0 ] && { echo -e "\\033[1;31mCan't checkout branch master, aborting\\0033[0;39m"; exit; }
echo -e "\\033[1;32mFetch upstream repo\\0033[0;39m"
git fetch upstream
echo -ne "\\033[1;32mGit $action upstream > master :\\0033[0;39m "
git $action upstream/master
echo -ne "\\033[1;32mGit push master > origin :\\0033[0;39m "
git push origin master
[ "$current_branch" != "master" ] && git checkout $current_branch
else
echo -e "\\033[1;31mPlease add upstream remote with command:\\0033[0;39m git remote add upstream [url]"
fi
else
echo "Not a git repo!"
fi
@atika
Copy link
Author

atika commented Dec 23, 2014

By default this bash script fetch upstream/master and merge modifications with master branch, and pull modifications to remote origin/master (stay forked repo in sync with upstream).
💥 Be careful if you choose rebase option that no prompt will tell you if you want to rebase the project and lose local modification !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment