Git good practices

What is a Pull Request (PR)?

First of all, what is a Pull Request (PR)? A PR is a method used to contribute to a project by proposing the modifications that the developer wishes to make on the main repository. It is the most used contribution method with decentralized version management tools (such as Git) and the most widely used thanks to GitHub and Open Source projects. Indeed, in the Open Source world, PR is very frequently used when a developer wishes to contribute to a project, whether to improve it and / or help in its development. For example, some will add documentation, translation, tests, while others will offer code enhancements or add functionality.

The main objective of a PR is to discuss the proposed changes with the people responsible for the original repository to be sure that the quality of their work is maintained. We submit these changes to the depot managers that can accept these changes by merging them and close the PR.

Note - It is important to understand that PR is a working method, it is not a functionality of the versioning system.

What is a Squash?

A “squash” is a grouping of several commits. The goal is to merge them into one to have a cleaner Git history. It is particularly advisable to do this as part of the Pull Request to simplify the rereading of the modifications made.

Concretely, when we work on a PR, we often make a lot of commits, whether to develop functionality, or to correct the feedback we get, we are easily left with ten or so commits. The main interest of the commits squash is to keep a clean history despite the many commits that have been made before. We will thus be able to group them all into a single commit containing all of the modifications. Our PR will be simpler and clearer.

What is a gitignore?

This is a small file that will allow git to ignore certain files. Its operation is very simple. It works by pattern, each folder or file which corresponds to a pattern present in the gitignore will be ignored by git. It influences the folder in which it is present and all the sub-folders. Several gitignore can overload themselves.

Github offers a very practical repository with many examples of .gitignore depending on the project you are working on that you can complete according to your needs and throughout the life of your repository:

What is a branch[1]?

Git allows you to use branches, You have to use and abuse them. For the simple reason that people will probably re-read your code behind with the Pull Request system and even if a feature is not finished, it will be able to re-read your code upstream and give you feedback more easily.

In addition, you will have a much clearer history of the features that have been developed. This will make it possible to separate the developments well even if you work alone, for example if you need to quickly correct a bug, you will be able to "pause" the development in progress and correct the bug in a transparent manner for your progress.

The main branches

At the core, the development model is greatly inspired by existing models out there. The central repo holds two main branches with an infinite lifetime:

  • master
  • develop

The main branches

The main branches [1]

The master branch at origin should be familiar to every Git user. Parallel to the master branch, another branch exists called develop.

  • We consider origin/master to be the main branch where the source code of HEAD always reflects a production-ready state.
  • We consider origin/develop to be the main branch where the source code of HEAD always reflects a state with the latest delivered development changes for the next release. Some would call this the “integration branch”. This is where any automatic nightly builds are built from.

When the source code in the develop branch reaches a stable point and is ready to be released, all of the changes should be merged back into master somehow and then tagged with a release number. How this is done in detail will be discussed further on. Therefore, each time when changes are merged back into master, this is a new production release by definition. We tend to be very strict at this, so that theoretically, we could use a Git hook script to automatically build and roll-out our software to our production servers everytime there was a commit on master.

Supporting branches

Next to the main branches master and develop, our development model uses a variety of supporting branches to aid parallel development between team members, ease tracking of features, prepare for production releases and to assist in quickly fixing live production problems. Unlike the main branches, these branches always have a limited life time, since they will be removed eventually.

The different types of branches we may use are:

  • Feature branches
  • Release branches
  • Hotfix branches

Each of these branches have a specific purpose and are bound to strict rules as to which branches may be their originating branch and which branches must be their merge targets. We will walk through them in a minute.

By no means are these branches “special” from a technical perspective. The branch types are categorized by how we use them. They are of course plain old Git branches.

Feature branches

May branch off from:

  • develop

Must merge back into:

  • develop

Branch naming convention:

  • anything except master, develop, release-*, or hotfix-*

The Feature branches

The Feature branches [1]

Feature branches (or sometimes called topic branches) are used to develop new features for the upcoming or a distant future release. When starting development of a feature, the target release in which this feature will be incorporated may well be unknown at that point. The essence of a feature branch is that it exists as long as the feature is in development, but will eventually be merged back into develop (to definitely add the new feature to the upcoming release) or discarded (in case of a disappointing experiment).

Feature branches typically exist in developer repos only, not in origin.

Release branches

May branch off from:

  • develop

Must merge back into:

  • develop and master

Branch naming convention:

  • release-*

Release branches support preparation of a new production release. They allow for last-minute dotting of i’s and crossing t’s. Furthermore, they allow for minor bug fixes and preparing meta-data for a release (version number, build dates, etc.). By doing all of this work on a release branch, the develop branch is cleared to receive features for the next big release.

The key moment to branch off a new release branch from develop is when develop (almost) reflects the desired state of the new release. At least all features that are targeted for the release-to-be-built must be merged in to develop at this point in time. All features targeted at future releases may not—they must wait until after the release branch is branched off.

It is exactly at the start of a release branch that the upcoming release gets assigned a version number—not any earlier. Up until that moment, the develop branch reflected changes for the “next release”, but it is unclear whether that “next release” will eventually become 0.3 or 1.0, until the release branch is started. That decision is made on the start of the release branch and is carried out by the project’s rules on version number bumping.

Hotfix branches

May branch off from:

  • master

Must merge back into:

  • develop and master

Branch naming convention:

  • hotfix-*

The Hotfix branches

The Hotfix branches [1]

Hotfix branches are very much like release branches in that they are also meant to prepare for a new production release, albeit unplanned. They arise from the necessity to act immediately upon an undesired state of a live production version. When a critical bug in a production version must be resolved immediately, a hotfix branch may be branched off from the corresponding tag on the master branch that marks the production version.

The essence is that work of team members (on the develop branch) can continue, while another person is preparing a quick production fix.

What is a Gitflow[1]?

Many open-source teams and projects adopt the Gitflow methodology. Gitflow offers a workflow for using git, a separation of branches and functionalities. Very practical for working with others, it makes it possible to standardize the use of the branches. I strongly invite you to take a look at how it works:

Gitflow

Git branching model [1]

If your functionality is substantial, do not hesitate to split your functionality into several branches, this will facilitate proofreading for a potential collaborator and thus accelerate the production of your work.

Sources

[1] https://nvie.com/posts/a-successful-git-branching-model/

Commentaires

Posts les plus consultés de ce blog

Knative vs OpenFaaS: What are the differences?

Remove .DS_Store from git repository

How to increase Pods limit per worker node in Kubernetes