---
title: Properly handle Git flow
date: 2022-09-11
slug: properly-handle-git-flow
authors:
- lunik
description: Full example of a Git repository lifecycle
tags:
- git
- gitflow
- merge request
- pull request
- feature
- merge
- pull
- request
- branche
- rebase
- checkout
---

<!--
# CHANGELOG

-->

![cover](/blog/img/posts/2022-09-11-properly-handle-git-flow/cover.png)

Any developper use or will use [Git][git-website] at a point in is career. Most of the time they will have to work with other people on the same [Git][git-website] repository. To avoid it to be branch and commit battlefield here is a simple guide on how to contribute properly on a [Git][git-website] repository.

<!-- truncate -->

## Basics

First of all, the [Git][git-website] repository should have a default branch often called `master` or `main` (it may be anything else as long as everyone agree on the name).

Secondly, a second branch used for development purpose. This one will follow the default branch pretty closely. It will be a receptacle for any new development. This branch is the only one allowed to be merged on the default branch.

Finally, all other branch fall into the last category. They are features, bugs fix and other.

**Note :** Only HotFix branches are allowed to bypass the development branch.

Here is an example of a simple [Git][git-website] repository

![example-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/01_example-git-repository-graph.png)

## Full example of a repository lifecycle

### Initializing a repository

The first thing to do when creating a new repository, is to initialize the base structure.

Create the [Git][git-website] repository :

```shell
mkdir -p myrepository

cd myrepository

git init
```

You may want to create your first files. Like the `README.md`/`.gitignore` and some package file like `package.json` or `pom.xml`.

Then create the initial commit of the repository with those files : 

```shell
git add .
git commit -m "Initial commit"
git tag v0.0.0
```

Finally create the development branch :

```shell
git branch develop
```

The repository should looks like :

![init-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/02_init-git-repository-graph.png)

### Initializing the development branch

Now that you have ou base structure on the [Git][git-website] repository, it's time initialize the development branch.

First checkout to the development branch :

```shell
git checkout develop
```

Sometimes you may want to add an initial commit on the development branch with the modification of the current version in your package file `package.json`, `pom.xml` or other.

Edit thoses files then create a commit :

```shell
git add .
git commit -m "Prepare development version"
```

The repository should looks like :

![init-develop-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/03_init-develop-git-repository-graph.png)

### Add the first feature

Now let's create the first feature in our application.

Verify that your are on the development branch :

```shell
git branch
```

Result :

```shell
* develop
  master
```

If not checkout on the development branch.

Then checkout to a feature branch :

```shell
git checkout -b feature/my-first-feature
```

Now write the feature and do commit from time to time with :

```shell
git add .
git commit -m "save dev"
```

The repository should looks like :

![feature-branch-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/04_feature-branch-git-repository-graph.png)

### Work on a required feature

You where working on the first feature but you realize that you needed another one to continue.

**/!\ Make sure that you don't have any unstaged changes before switching branches**

Reproduce the same commands as for the first feature : 
- Checkout to the development branch
- Then checkout to a feature branch
- Write your code and create commits

```shell
git checkout develop
git checkout -b feature/my-required-feature
```

The repository should looks like :

![feature2-branch-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/05_feature2-branch-git-repository-graph.png)

### Merging the required feature

Now that you have finished your work on the required feature comes the time to merge the code to the development branch.

First you need to rebase the branch to remove all unecessary commits :

```shell
git rebase -i develop
```

The first commit should always be picked. All other commits can be squashed.

```shell
pick fa71872 save dev
squash 5d9a97a save dev
```

The repository should looks like :

![feature2-rebase-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/06_feature2-rebase-git-repository-graph.png)

Checkout to the development branch and merge the require feature branch :

```shell
git checkout develop
git merge --no-ff feature/my-required-feature
```

The repository should looks like :

![feature2-merge-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/07_feature2-merge-git-repository-graph.png)

### Continuing the work on the first feature

Now that you have finished the required feature, you want to rebase your current work on the first feature to retrieve the content of the required feature.

- Checkout to the feature branch
- Rebase it on the developement branch

```shell
git checkout feature/my-first-feature
git rebase develop
```

What this command is doing is taking all the commit from the feature branch and apply them at the end of the developement branch.

The repository should looks like :

![feature-rebase-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/08_feature-rebase-git-repository-graph.png)

### Merging the first feature

Now that you have finished your work on the first feature comes the time to merge the code to the development branch.

First you need to rebase the branch to remove all unecessary commits :

```shell
git rebase -i develop
```

The first commit should always be picked. All other commits can be squashed.

```shell
pick 717051b save dev
squash 7d39273 save dev
squash 7b700f1 save dev
```

The repository should looks like :

![feature-rebase2-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/09_feature-rebase2-git-repository-graph.png)

Checkout to the development branch and merge the require feature branch :

```shell
git checkout develop
git merge --no-ff feature/my-first-feature
```

The repository should looks like :

![feature-merge-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/10_feature-merge-git-repository-graph.png)

### Hotfix critical issue

A critical issue have been discovered on the production application an you need to quickly produce a patch. You don't have the time to go through all the release process.

- Checkout from the default branch
- Create a new hotfix branch and checkout to it
- Make the correction
- Merge into the default branch
- Tag your new release

The repository should looks like :

![hotfix-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/11_hotfix-git-repository-graph.png)

### Releasing your work

It's now time to release all that hard work.

First you need to merge the default branch to retreiv all hotfix corrections.

```shell
git checkout develop
git merge --no-ff master
```

The repository should looks like :

![develop-rebase-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/12_develop-rebase-git-repository-graph.png)

And finaly, merge your development on the default branch.

```shell
git checkout master
git merge --no-ff develop
```

The repository should looks like :

![develop-merge-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/13_develop-merge-git-repository-graph.png)

### Cleanup

Let's do some cleanup by removing some unused references like feature and hotfix branches.

```shell
git branch -D feature/my-first-feature
git branch -D feature/my-required-feature
git branch -D hotfix/correct-critical-issue  
```

The repository should looks like :

![cleanup-git-repository-graph](/blog/img/posts/2022-09-11-properly-handle-git-flow/14_cleanup-git-repository-graph.png)

## Notes

All Git graph have been generated with [Bit-Booster app][bit-booster-website]

<!-- links -->

[git-website]: https://git-scm.com
[bit-booster-website]: https://bit-booster.com/