How do I update a GitHub forked repository?

前端 未结 23 2851
借酒劲吻你
借酒劲吻你 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=
    MASTER="git@github.com:/$REPOSITORY.git"   
    UPSTREAM=git@github.com:/.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 :master.

提交回复
热议问题