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="[email protected]:<username>/$REPOSITORY.git"
[email protected]:<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.