Fast-forward your GitHub fork

I have read many blogs, dig deeply in the GitHub site and Git references,... but I have not found any satisfactory solution to my really simple requirements: keep the forked repository in sync easily, and be able to push to the upstream project with simplicity. So I have finally written my own:

  1. Fork the upstream repository on GitHub
    2. git clone -o upstream <upstream url>; cd <wc folder>
    3. git branch -t <upstream branch> upstream/<upstream url>
    4. git remote add origin <your fork url>
    5. git fetch origin
    6. git push origin master
    7. git push origin <upstream branch>
    8. git checkout -b <my branch> <any branch or master>
    9. git push -u origin <my branch>

Setup this way, your working copy can easily keep in sync the master branch of your fork with the master branch of upstream, as well as any branches you are tracking from upstream, using fast-forward pulls BUT IF YOU DO NOT HAVE WRITE ACCESS TO UPSTREAM, NEVER COMMIT ANYTHING ON MASTER OR ANY UPSTREAM TRACKING BRANCH.

Pulling from upstream will fetch and merge master and any upstream branches you are tracking. Pushing to upstream will also allow you to push commits upstream if you are allowed to do so.

Pulling from origin will fetch your branches from your fork. Pushing to origin will push master, existing upstream branches and your own tracked branches to your forked repository.

If you have write access on upstream, you may merge your changes to master or an upstream branch and push it on both upstream and origin. To preserve a fast-forward history as much as possible, do not forget to pull before merging, or to pull --rebase if you are (still) in trouble. AND NEVER PUSH MASTER TO ORIGIN WITHOUT FIRST PUSHING IT TO UPSTREAM.

For those who will think that I am shouting too much since Git  means never having to say, “you should have”: if you do not apply my warning carefully, you will be in trouble with  other users since you will have to revert some commits that you have pushed on a remote, but you have in Git all you need to fix that, and get back in sync.