How do I update a GitHub forked repository?

前端 未结 23 2823
借酒劲吻你
借酒劲吻你 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 12:00

    Starting in May 2014, it is possible to update a fork directly from GitHub. This still works as of September 2017, BUT it will lead to a dirty commit history.

    1. Open your fork on GitHub.
    2. Click on Pull Requests.
    3. Click on New Pull Request. By default, GitHub will compare the original with your fork, and there shouldn't be anything to compare if you didn't make any changes.
    4. Click switching the base if you see that link. Otherwise, manually set the base fork drop down to your fork, and the head fork to the upstream. Now GitHub will compare your fork with the original, and you should see all the latest changes.
    5. Create pull request and assign a predictable name to your pull request (e.g., Update from original).
    6. Scroll down to Merge pull request, but don't click anything yet.

    Now you have three options, but each will lead to a less-than-clean commit history.

    1. The default will create an ugly merge commit.
    2. If you click the dropdown and choose "Squash and merge", all intervening commits will be squashed into one. This is most often something you don't want.
    3. If you click Rebase and merge, all commits will be made "with" you, the original PRs will link to your PR, and GitHub will display This branch is X commits ahead, Y commits behind <original fork>.

    So yes, you can keep your repo updated with its upstream using the GitHub web UI, but doing so will sully your commit history. Stick to the command line instead - it's easy.

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

    Here is GitHub's official document on Syncing a fork:

    Syncing a fork

    The Setup

    Before you can sync, you need to add a remote that points to the upstream repository. You may have done this when you originally forked.

    Tip: Syncing your fork only updates your local copy of the repository; it does not update your repository on GitHub.

    $ git remote -v
    # List the current remotes
    origin  https://github.com/user/repo.git (fetch)
    origin  https://github.com/user/repo.git (push)
    
    $ git remote add upstream https://github.com/otheruser/repo.git
    # Set a new remote
    
    $ git remote -v
    # Verify new remote
    origin    https://github.com/user/repo.git (fetch)
    origin    https://github.com/user/repo.git (push)
    upstream  https://github.com/otheruser/repo.git (fetch)
    upstream  https://github.com/otheruser/repo.git (push)
    

    Syncing

    There are two steps required to sync your repository with the upstream: first you must fetch from the remote, then you must merge the desired branch into your local branch.

    Fetching

    Fetching from the remote repository will bring in its branches and their respective commits. These are stored in your local repository under special branches.

    $ git fetch upstream
    # Grab the upstream remote's branches
    remote: Counting objects: 75, done.
    remote: Compressing objects: 100% (53/53), done.
    remote: Total 62 (delta 27), reused 44 (delta 9)
    Unpacking objects: 100% (62/62), done.
    From https://github.com/otheruser/repo
     * [new branch]      master     -> upstream/master
    

    We now have the upstream's master branch stored in a local branch, upstream/master

    $ git branch -va
    # List all local and remote-tracking branches
    * master                  a422352 My local commit
      remotes/origin/HEAD     -> origin/master
      remotes/origin/master   a422352 My local commit
      remotes/upstream/master 5fdff0f Some upstream commit
    

    Merging

    Now that we have fetched the upstream repository, we want to merge its changes into our local branch. This will bring that branch into sync with the upstream, without losing our local changes.

    $ git checkout master
    # Check out our local master branch
    Switched to branch 'master'
    
    $ git merge upstream/master
    # Merge upstream's master into our own
    Updating a422352..5fdff0f
    Fast-forward
     README                    |    9 -------
     README.md                 |    7 ++++++
     2 files changed, 7 insertions(+), 9 deletions(-)
     delete mode 100644 README
     create mode 100644 README.md
    

    If your local branch didn't have any unique commits, git will instead perform a "fast-forward":

    $ git merge upstream/master
    Updating 34e91da..16c56ad
    Fast-forward
     README.md                 |    5 +++--
     1 file changed, 3 insertions(+), 2 deletions(-)
    

    Tip: If you want to update your repository on GitHub, follow the instructions here

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

    Android Studio now has learned to work with GitHub fork repositories (you don't even have to add "upstream" remote repository by console command).

    Open menu VCSGit

    And pay attention to the two last popup menu items:

    • Rebase my GitHub fork

    • Create Pull Request

    Try them. I use the first one to synchronize my local repository. Anyway the branches from the parent remote repository ("upstream") will be accessible in Android Studio after you click "Rebase my GitHub fork", and you will be able to operate with them easily.

    (I use Android Studio 3.0 with "Git integration" and "GitHub" plugins.)

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

    Actually, it is possible to create a branch in your fork from any commit of the upstream in the browser:

    • Open https://github.com/<repo>/commits/<hash>, where repo is your fork, and hash is full hash of commit which you can find in the upstream web interface. For example, I can open https://github.com/max630/linux/commits/0aa0313f9d576affd7747cc3f179feb097d28990, which points to linux master as time of writing.
    • Click on the "Tree: ...." button.
    • Type name of the new branch and press Enter

    You can then fetch that branch to your local clone, and you won't have to push all that data back to GitHub when you push edits on top of that commit. Or use the web interface to change something in that branch.

    How it works (it is a guess, I don't know how exactly GitHub does it): forks share object storage and use namespaces to separate users' references. So you can access all commits through your fork, even if they did not exist by the time of forking.

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

    Follow the below steps. I tried them and it helped me.

    Checkout to your branch

    Syntax: git branch yourDevelopmentBranch
    Example: git checkout master

    Pull source repository branch for getting the latest code

    Syntax: git pull https://github.com/tastejs/awesome-app-ideas master
    Example: git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git BRANCH_NAME

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

    A lot of answers end up moving your fork one commit ahead of the parent repository. This answer summarizes the steps found here which will move your fork to the same commit as the parent.

    1. Change directory to your local repository.

      • Switch to master branch if you are not git checkout master
    2. Add the parent as a remote repository, git remote add upstream <repo-location>

    3. Issue git fetch upstream
    4. Issue git rebase upstream/master

      • At this stage you check that commits what will be merged by typing git status
    5. Issue git push origin master

    For more information about these commands, refer to step 3.

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