Skip to content

Instantly share code, notes, and snippets.

@LongJohnCoder
Forked from ariporad/README.md
Created February 5, 2017 15:05
Show Gist options
  • Select an option

  • Save LongJohnCoder/651a3335a49cad0f64d745c911c230aa to your computer and use it in GitHub Desktop.

Select an option

Save LongJohnCoder/651a3335a49cad0f64d745c911c230aa to your computer and use it in GitHub Desktop.
git-refork

git-refork

I really wanted a way to re-sync a fork with upstream, so this is it. Just put it in your path, and make sure that you have a remote called upstream that points to the original project.

WARNING: For forks only. If you use it on a non-fork project, bad things will happen. This will re-write history, so be careful.

#!/bin/bash
##
# ATTENTION: If you've stumbled upon this and have no clue what it is, see here: http://git.io/vErAh.
##
##
# Welcome to git-refork! git-refork is designed to re-sync forks with upstream, by rebasing master onto upstream/master,
# Force pushing, then rebasing your curent branch onto the new master, and force pushing.
#
# It requires that you have the upstream of your project configured as a remote called `upstream`.
#
# Put it in your $PATH, then use it as `git refork`.
##
USAGE="Rebases master onto upstream/master, force pushes to origin, Rebases current branch unto master, force pushes"
SUBDIRECTORY_OK=1
. "$(git --exec-path)/git-sh-setup"
BRANCH_NAME=$(git describe --contains --all HEAD)
print() {
printf "\n$@\n"
}
echo "Reforking..."
print "Switching to master:"
git checkout master
print "Fetching remotes:"
git fetch origin
git fetch upstream
print "Rebasing onto upstream/master"
git rebase upstream/master
print "Force-pushing to origin/master"
git push -f
if [[ $BRANCH_NAME != "master" ]]; then
print "Switching to $BRANCH_NAME"
git checkout $BRANCH_NAME
print "Rebasing onto master"
git rebase master
print "Force-Pushing to origin/$BRANCH_NAME"
git push -f
else
print "You were originally on master, doing nothing else."
fi
print "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment