Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean up a fork and restart it from the upstream

Tags:

git

I have forked a repository, then I made some changes and it looks like I've messed up everything.

I wish to start it again from scratch, using the current upstream/master as the base for my work.
Should I rebase my repository or delete it at all?

like image 651
tampe125 Avatar asked Mar 10 '12 11:03

tampe125


People also ask

How do I get a fork branch from upstream?

Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git. Open TerminalTerminalGit Bash. Change the current working directory to your local project. Fetch the branches and their respective commits from the upstream repository.

How do I reset my upstream?

You can reset your local master branch to the upstream version and push it to your origin repository. (You can define the original repo as "upstream" with git remote add upstream /url/to/original/repo .) It should probably be git reset --hard upstream/master to reset the working directory, too.


2 Answers

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.

like image 83
VonC Avatar answered Sep 18 '22 20:09

VonC


Love VonC's answer. Here's an easy version of it for beginners.

There is a git remote called origin which I am sure you are all aware of. Basically, you can add as many remotes to a git repo as you want. So, what we can do is introduce a new remote which is the original repo not the fork. I like to call it original

Let's add original repo's to our fork as a remote.

git remote add original https://git-repo/original/original.git 

Now let's fetch the original repo to make sure we have the latest coded

git fetch original 

As, VonC suggested, make sure we are on the master.

git checkout master 

Now to bring our fork up to speed with the latest code on original repo, all we have to do is hard reset our master branch in accordance with the original remote.

git reset --hard original/master 

And you are done :)

like image 20
Ahmad Awais Avatar answered Sep 20 '22 20:09

Ahmad Awais