Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reset the git master branch to the upstream branch in a forked repository?

Tags:

git

People also ask

How do I set up upstream branch?

The easiest way to set the upstream branch is to use the “git push” command with the “-u” option for upstream branch. Alternatively, you can use the “–set-upstream” option that is equivalent to the “-u” option. As an example, let's say that you created a branch named “branch” using the checkout command.


You can reset your local master branch to the upstream version and push it to your origin repository.

Assuming that "upstream" is the original repository and "origin" is your fork:

# ensures current branch is master
git checkout master

# pulls all new commits made to upstream/master
git pull upstream master

# this will delete all your local changes to master
git reset --hard upstream/master

# take care, this will delete all your changes on your forked master
git push origin master --force

(You can define the original repo as "upstream" with git remote add upstream /url/to/original/repo.)


This would reset your master branch with the upstream master and if the branch has been updated since your forked it would pull those changes as well.

git checkout master 
git reset upstream/master
git pull --rebase upstream master
git push origin master --force

PS: Assuming Upstream is the original repo while origin is your copy.


git reset --hard @{upstream}

or, shorter:

git reset --hard @{u}

Or you can even take it one step further and setup an alias that will allow you to simply type git scrub:

git config --global alias.scrub 'reset --hard @{upstream}'

(This assumes that your branch is configured to track the corresponding remote branch, which it typically is, unless you are doing something special. See git-branch(1) for more details on tracking and git-rev-parse(1) for details on the branch specification syntax.)

And then you just git push --force to your fork, as explained in other answers.