Clean up a fork and restart it from the upstream

The simplest solution would be (using ‘upstream‘ as the remote name referencing the original repo forked):

git remote add upstream /url/to/original/repo
git fetch upstream
git checkout master
git reset --hard upstream/master  
git push origin master --force 

(Similar to this GitHub page, section “What should I do if I’m in a bad situation?”)

Be aware that you can lose changes done on the master branch (both locally, because of the reset --hard, and on the remote side, because of the push --force).

An alternative would be, if you want to preserve your commits on master, to replay those commits on top of the current upstream/master.
Replace the reset part by a git rebase upstream/master. You will then still need to force push.
See also “What should I do if I’m in a bad situation?”


A more complete solution, backing up your current work (just in case) is detailed in “Cleanup git master branch and move some commit to new branch”.

See also “Pull new updates from original GitHub repository into forked GitHub repository” for illustrating what “upstream” is.

upstream


Note: recent GitHub repos do protect the master branch against push --force.
So you will have to un-protect master first (see picture below), and then re-protect it after force-pushing).

enter image description here


Note: on GitHub specifically, there is now (February 2019) a shortcut to delete forked repos for pull requests that have been merged upstream.

Leave a Comment