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

后端 未结 4 2086
轮回少年
轮回少年 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 17:08

    I have tried the method like this:

    $REPO=
    $ORIGIN=/$REPO
    $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  [...] -- [...]'
    

    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:/
     * branch                master     -> FETCH_HEAD
     * [new branch]          master     -> upstream/master
    HEAD is now at 7a94b1790 Merge pull request #4237 from /...
    Current branch master is up to date.
    Everything up-to-date.
    

提交回复
热议问题