How do I update a GitHub forked repository?

前端 未结 23 2781
借酒劲吻你
借酒劲吻你 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:45

    There are two main things on keeping a forked repository always update for good.

    1. Create the branches from the fork master and do changes there.

    So when your Pull Request is accepted then you can safely delete the branch as your contributed code will be then live in your master of your forked repository when you update it with the upstream. By this your master will always be in clean condition to create a new branch to do another change.

    2. Create a scheduled job for the fork master to do update automatically.

    This can be done with cron. Here is for an example code if you do it in linux.

    $ crontab -e
    

    put this code on the crontab file to execute the job in hourly basis.

    0 * * * * sh ~/cron.sh
    

    then create the cron.sh script file and a git interaction with ssh-agent and/or expect as below

    #!/bin/sh
    WORKDIR=/path/to/your/dir   
    REPOSITORY=<name of your repo>
    MASTER="git@github.com:<username>/$REPOSITORY.git"   
    UPSTREAM=git@github.com:<upstream>/<name of the repo>.git  
    
    cd $WORKDIR && rm -rf $REPOSITORY
    eval `ssh-agent` && expect ~/.ssh/agent && ssh-add -l
    git clone $MASTER && cd $REPOSITORY && git checkout master
    git remote add upstream $UPSTREAM && git fetch --prune upstream
    if [ `git rev-list HEAD...upstream/master --count` -eq 0 ]
    then
        echo "all the same, do nothing"
    else
        echo "update exist, do rebase!"
        git reset --hard upstream/master
        git push origin master --force
    fi
    cd $WORKDIR && rm -rf $REPOSITORY
    eval `ssh-agent -k`
    

    Check your forked repository. From time to time it will always show this notification:

    This branch is even with <upstream>:master.

    0 讨论(0)
  • 2020-11-21 11:45
    rm -rf oldrepository
    git clone ...
    

    There may be subtler options, but it is the only way that I have any confidence that my local repository is the same as upstream.

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

    If you use GitHub Desktop, you can do it easily in just 6 steps (actually only 5).

    Once you open Github Desktop and choose your repository,

    1. Go to History tab
    2. Click on the search bar. It will show you all the available branches (including upstream branches from parent repository)
    3. Select the respective upstream branch (it will be upstream/master to sync master branch)
    4. (OPTIONAL) It will show you all the commits in the upstream branch. You can click on any commit to see the changes.
    5. Click Merge in master / branch-name, based on your active branch.
    6. Wait for GitHub Desktop to do the magic.

    Checkout the GIF below as an example:

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

    That depends on the size of your repository and how you forked it.

    If it's quite a big repository you may have wanted to manage it in a special way (e.g. drop history). Basically, you can get differences between current and upstream versions, commit them and then cherry pick back to master.

    Try reading this one. It describes how to handle big Git repositories and how to upstream them with latest changes.

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

    Since November 2013 there has been an unofficial feature request open with GitHub to ask them to add a very simple and intuitive method to keep a local fork in sync with upstream:

    https://github.com/isaacs/github/issues/121

    Note: Since the feature request is unofficial it is also advisable to contact support@github.com to add your support for a feature like this to be implemented. The unofficial feature request above could be used as evidence of the amount of interest in this being implemented.

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

    The "Pull" app is an automatic set-up-and-forget solution. It will sync the default branch of your fork with the upstream repository.

    Visit the URL, click the green "Install" button and select the repositories where you want to enable automatic synchronization.

    The branch is updated once per hour directly on GitHub, on your local machine you need to pull the master branch to ensure that your local copy is in sync.

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