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

后端 未结 4 2073
轮回少年
轮回少年 2020-12-22 16:24

I\'ve completely messed up the master branch of my forked git repo.

I want to completely reset the master branch that was pushed to my fork with the contents of the

相关标签:
4条回答
  • 2020-12-22 16:51

    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.

    0 讨论(0)
  • 2020-12-22 17:06

    You can reset your local master branch to the upstream version and push it to your your 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.)

    0 讨论(0)
  • 2020-12-22 17:08

    I have tried the method like this:

    $REPO=<repo>
    $ORIGIN=<user>/$REPO
    $UPSTREAM=<upstream>/$REPO
    
    $ git clone git@github.com:$ORIGIN.git
    $ cd $REPO
    $ git checkout master
    $ git remote add upstream git@github.com:$UPSTREAM.git
    $ git reset --hard upstream/master
    $ git pull --rebase upstream master
    $ git push origin master --force
    

    the output will show a warning:

    fatal: ambiguous argument 'upstream/master': 
    unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'
    

    So the correct way is put git pull before git reset:

    $ git clone git@github.com:$ORIGIN.git
    $ cd $REPO
    $ git checkout master
    $ git remote add upstream git@github.com:$UPSTREAM.git
    $ git pull --rebase upstream master
    $ git reset --hard upstream/master
    $ git push origin master --force
    

    then the output will be like this:

    From github.com:<upstream>/<repo>
     * branch                master     -> FETCH_HEAD
     * [new branch]          master     -> upstream/master
    HEAD is now at 7a94b1790 Merge pull request #4237 from <upstream>/...
    Current branch master is up to date.
    Everything up-to-date.
    
    0 讨论(0)
  • 2020-12-22 17:09
    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.

    0 讨论(0)
提交回复
热议问题