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?
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.
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.
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.
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).
Note: on GitHub specifically, there is now (February 2019) a shortcut to delete forked repos for pull requests that have been merged upstream.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With