Skip to content

Instantly share code, notes, and snippets.

@glennblock
Created March 4, 2012 19:27
Show Gist options
  • Save glennblock/1974465 to your computer and use it in GitHub Desktop.
Save glennblock/1974465 to your computer and use it in GitHub Desktop.
Force your forked repo to be the same as upstream.
git fetch upstream
git reset --hard upstream/master
@anaisbetts
Copy link

Keep in mind, that this makes your repo look exactly like upstream - if you have commits, they will be eaten! If you want to update to upstream (but still keep your commits), you want git merge upstream/master. If you want to start a new patch based on upstream, the best way to do it is git checkout -b my-new-feature upstream/master

Of course, this all expects you have a remote named upstream, which you can create by doing git remote add upstream https://[upstream guy's repo url]

@anaisbetts
Copy link

(Just to clarify, the term 'upstream' means the person you forked from)

@dterweij
Copy link

dterweij commented Feb 4, 2014

[danny@server01 kloxo]$ git fetch upstream
[danny@server01 kloxo]$ git reset --hard upstream/dev
fatal: ambiguous argument 'upstream/dev': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'

And now?

@pepa65
Copy link

pepa65 commented Feb 8, 2017

So this works great locally, but how can I get my github repo master branch to be the same as locally/upstream??

@shacker
Copy link

shacker commented Feb 18, 2017

@pepa65
git push origin master --force

@GorvGoyl
Copy link

GorvGoyl commented Nov 1, 2017

complete git commands would be like this:

git remote add upstream https://github.com/some_user/some_repo
git fetch upstream
git checkout master
git reset --hard upstream/master  
git push origin master --force

@chsuditi
Copy link

chsuditi commented Dec 8, 2017

worked for me..thnx

@bls1999
Copy link

bls1999 commented Feb 14, 2018

@JerryGoyal PRAISE YOU

@azzamsa
Copy link

azzamsa commented Mar 18, 2018

@garrettbear
Copy link

@JerryGoyal thank you kind sir

@amamsaang
Copy link

👍

@jtr13
Copy link

jtr13 commented Dec 10, 2018

Why not just delete the fork and start over?

@ravi-bharadwaj
Copy link

@JerryGoyal, Thank you!

@PatMyron
Copy link

@jtr13 might have Github issues you want to keep around

@kbintermx
Copy link

So how about the opposite situation?

What if the history was changed on a fork but it needs to be asserted as the upstream master?
ie, the error: "There isn’t anything to compare." A:master and B: "are entirely different commit histories."

Precondition: Working on the fork

git fetch origin
git checkout master
git reset --hard origin/master
git remote add upstream https://github.com/some_user/A
git push upstream master --force

Would this have expected results?

@vaibhavhrt
Copy link

git push --force is a required step too to update your remote i.e. origin/master after the hard reset

@michaelshiyu
Copy link

Works like a charm. Thanks!

@108krohan
Copy link

This is so great, thank you! @JerryGoyal

@andre91998
Copy link

what if there are new files in my commit that I want to keep, but changes the upstream made to files that I already have that I also want. When I try to merge upstream, I receive an error: files would be overwritten by merge. But I want them to be overwritten.

@bilogic
Copy link

bilogic commented Mar 22, 2021

can we do this from the GitHub UI?

@rzvn2600
Copy link

rzvn2600 commented Apr 1, 2021

How can I force users to pull from upstream after a major upgrade of the source code. I have PR validation in place (Jenkins) and they are using the old validation code (python).. ?

@Evavic44
Copy link

Works perfectly. Thanks for the tip.

@skeetmtp
Copy link

skeetmtp commented Mar 16, 2022

you may need to also push tags

git push --tags

@Steveantor
Copy link

other variations:

git reset --hard upstream/master
git reset --hard upstream/main
git reset --hard upstream/development

@solalatus
Copy link

👍

@ChinYikMing
Copy link

complete git commands would be like this:

git remote add upstream https://github.com/some_user/some_repo
git fetch upstream
git checkout master
git reset --hard upstream/master  
git push origin master --force

Perfect! Thank you!

@poa00
Copy link

poa00 commented May 30, 2024

So this works great locally, but how can I get my GitHub repo master branch to be the same as locally/upstream??

This is possible with a one-liner that doesn't require downloading anything...

For anyone interested in syncing your GitHub-hosted fork directly (without cloning locally first) you can do this using the GitHub CLI:

gh repo sync owner/cli-fork -b BRANCH-NAME

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