|

How to maintain a fork on github efficiently

Forking or cloning a Git project on popular platforms like GitHub, GitLab, and Bitbucket is a common process and an important part of the opensource community.

But how to maintain and synchronize a fork with updates from the original project at any time if there are changes there?

This is easily possible via so-called remote repositories.
A remote repository is usually added to the local project with git remote add. This step is often done once at the very beginning of a project for origin, so that the local project points to the project at the corresponding platform.

The command looks like this:
git remote add <remotename> <url>.

The name is arbitrary and the URL points to the appropriate repository.

A concrete example looks like this:
git remote add upstream [email protected]:magento/magento2.git

By default I use the name upstream to point to the original repository I forked from.

After that, on the same branch, I can use git pull upstream branchname to fetch the changes from the specified remote repository upstream.

With fetch, rebase, merge you can synchronize your own fork relatively easy this way.

Ideally, the original branches should not be touched, or the changes from upstream should be integrated via merge --ff (fast-forward merge), merge --no-ff (with merge commit) or git pull --rebase.

The settings for the remotes are stored locally in the file .git/config.

So, in summary, the process is as follows:

git remote add upstream git@...
git pull upstream master

Or

git fetch upstream master
git rebase upstream master

Optionally you can also use the automatically created branch upstream/master directly, i.e. git rebase upstream/master.

The current remote repositories including URL can be listed with git remote -v.

For example, I had done this with post-mortems in my fork with the open pull requests, since the project was not maintained at the time. In that case, I added each fork of the original project as a remote repository and integrated the respective branch from the pull requests with merge --no-ff and submitted all the changes to the original project as a pull request.

git remote add matkoniecz [email protected]:matkoniecz/post-mortems.git
git fetch matkoniecz
git merge --no-ff matkoniecz/dedup

git remote add loganmeetsworld [email protected]:loganmeetsworld/post-mortems.git
git fetch loganmeetsworld
git merge --no-ff loganmeetsworld/homebrew

git remote add peterfroehlich [email protected]:peterfroehlich/post-mortems.git
git fetch peterfroehlich
git merge --no-ff peterfroehlich/patch-1

git remote add takirala [email protected]:takirala/post-mortems.git
git fetch takirala
git merge --no-ff takirala/patch-1
git push