How do I update a GitHub forked repository?

前端 未结 23 2659
借酒劲吻你
借酒劲吻你 2020-11-21 11:07

I recently forked a project and applied several fixes. I then created a pull request which was then accepted.

A few days later another change was made by another con

相关标签:
23条回答
  • 2020-11-21 11:51

    If you set your upstream. Check with git remote -v, then this will suffice.

    git fetch upstream
    git checkout master
    git merge --no-edit upstream/master
    git push
    
    0 讨论(0)
  • 2020-11-21 11:53

    Foreword: Your fork is the "origin" and the repository you forked from is the "upstream".

    Let's assume that you cloned already your fork to your computer with a command like this:

    git clone git@github.com:your_name/project_name.git
    cd project_name
    

    If that is given then you need to continue in this order:

    1. Add the "upstream" to your cloned repository ("origin"):

      git remote add upstream git@github.com:original_author/project_name.git
      
    2. Fetch the commits (and branches) from the "upstream":

      git fetch upstream
      
    3. Switch to the "master" branch of your fork ("origin"):

      git checkout master
      
    4. Stash the changes of your "master" branch:

      git stash
      
    5. Merge the changes from the "master" branch of the "upstream" into your the "master" branch of your "origin":

      git merge upstream/master
      
    6. Resolve merge conflicts if any and commit your merge

      git commit -am "Merged from upstream"
      
    7. Push the changes to your fork

      git push
      
    8. Get back your stashed changes (if any)

      git stash pop
      
    9. You're done! Congratulations!

    GitHub also provides instructions for this topic: Syncing a fork

    0 讨论(0)
  • 2020-11-21 11:54

    Use these commands (in lucky case)

    git remote -v
    git pull
    git fetch upstream
    git checkout master
    git merge upstream/master --no-ff
    git add .
    git commit -m"Sync with upstream repository."
    git push -v
    
    0 讨论(0)
  • 2020-11-21 11:55

    As a complement to this answer, I was looking for a way to update all remote branches of my cloned repo (origin) from upstream branches in one go. This is how I did it.

    This assumes you have already configured an upstream remote pointing at the source repository (where origin was forked from) and have synced it with git fetch upstream.

    Then run:

    for branch in $(git ls-remote --heads upstream|sed 's#^.*refs/heads/##'); do git push origin refs/remotes/upstream/$branch:refs/heads/$branch; done
    

    The first part of this command lists all heads in the upstream remote repo and removes the SHA-1 followed by refs/heads/ branch name prefix.

    Then for each of these branches, it pushes the local copy of the upstream remote tracking branch (refs/remotes/upstream/<branch> on local side) directly to the remote branch on origin (refs/heads/<branch> on remote side).

    Any of these branch sync commands may fail for one of two reasons: either the upstream branch have been rewritten, or you have pushed commits on that branch to your fork. In the first case where you haven't committed anything to the branch on your fork it is safe to push forcefully (Add the -f switch; i.e. git push -f in the command above). In the other case this is normal as your fork branch have diverged and you can't expect the sync command to work until your commits have been merged back into upstream.

    0 讨论(0)
  • 2020-11-21 11:56

    If, like me, you never commit anything directly to master, which you should really, you can do the following.

    From the local clone of your fork, create your upstream remote. You only need to do that once:

    git remote add upstream https://github.com/whoever/whatever.git
    

    Then whenever you want to catch up with the upstream repository master branch you need to:

    git checkout master
    git pull upstream master
    

    Assuming you never committed anything on master yourself you should be done already. Now you can push your local master to your origin remote GitHub fork. You could also rebase your development branch on your now up-to-date local master.

    Past the initial upstream setup and master checkout, all you need to do is run the following command to sync your master with upstream: git pull upstream master.

    0 讨论(0)
  • 2020-11-21 12:00

    In your local clone of your forked repository, you can add the original GitHub repository as a "remote". ("Remotes" are like nicknames for the URLs of repositories - origin is one, for example.) Then you can fetch all the branches from that upstream repository, and rebase your work to continue working on the upstream version. In terms of commands that might look like:

    # Add the remote, call it "upstream":
    
    git remote add upstream https://github.com/whoever/whatever.git
    
    # Fetch all the branches of that remote into remote-tracking branches
    
    git fetch upstream
    
    # Make sure that you're on your master branch:
    
    git checkout master
    
    # Rewrite your main branch so that any commits of yours that
    # aren't already in upstream/main are replayed on top of that
    # other branch:
    
    git rebase upstream/master
    

    If you don't want to rewrite the history of your main branch, (for example because other people may have cloned it) then you should replace the last command with git merge upstream/main. However, for making further pull requests that are as clean as possible, it's probably better to rebase.


    If you've rebased your branch onto upstream/main you may need to force the push in order to push it to your own forked repository on GitHub. You'd do that with:

    git push -f origin master
    

    You only need to use the -f the first time after you've rebased.

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