Skip to content

Instantly share code, notes, and snippets.

@ariporad
Last active December 7, 2017 17:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ariporad/a067e59d8f07d69ccf9d to your computer and use it in GitHub Desktop.
Save ariporad/a067e59d8f07d69ccf9d 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."
@phrohdoh
Copy link

There is 0 error checking or confirmation.
Please, no one use this the way it is.
git fetch and git push are simple commands you can master within a matter of minutes.

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