Using git with tracking branches

As you probably know already, Git is a fully distributed version control system. This means that there is no master repository in Git as such, only repositories that you can pull information from or push information to in order to advance in the development of a given project. The former are known as upstream repositories whereas the latter are known as downstream repositories.

Despite being completely distributed, it is typical for all Git projects to have an upstream repository that contains the master copy of the project source code. This is where all the changes contributed by developers are integrated, typically making sure that the code is always functional using continuous integration tools (e.g. Travis or Jenkins).

Considering such approach to development, it is usually required to pull information from an upstream repository to a local repository (e.g. a folder in your computer), where you will merge and modify it as required and, afterwards, push it to your remote repository (e.g. the copy of your local repository hosted in GitHub or BitBucket). Once there it is also typical to make a pull request to get your changes integrated back into the master repository, but this is another story.

Focusing on pulling information from an upstream repository, the first step is adding the address of the upstream repository in your local Git repository by issuing the command “git remote add upstream git://“.

After that you need to fetch changes from the upstream repository and merge them into your local repository. To do that it is convenient to add the following lines to the .gitconfig file in your local repository:

pu = !”git fetch origin -v; git fetch upstream -v; git merge upstream/master

This defines an alias that allows to do the fetch from the upstream repository and merge to the local repository in a single step by executing “git pu“. If Git is able to merge the changes in both repositories automatically all that will be left to do is push the changes back to your remote repository by issuing the “git push” command. If not you will need to integrate the changes manually and do the push afterwards.