Git branching and workflow explained
What is a GIT branch and what is the feature branch workflow ?
Git is very powerful and maybe the best tools for developers and for devops as well to keep versioning of all their IAC ( Infrastructure as code ) files and configuration like Ansible roles or Terraform config files.
By example, all the config files of this website are on git and once the code is commit, the site is automatically deploy on GitLab Pages using the pipeline of GitLab CI/CD.
Git make a big improvement in the life of the developers, one the main feature is to work with branches to make collaboration between them easier.
What is a Git branch ?
Basically, a branch is a pointer to your next commits and it’s an independent line of development for for new release, bugfix, new features, etc
You can think of them as a way to request a brand new working directory, staging area, and project history.
In other words, when you are using branch, the original code stay untouched and each developers can work on different branches.
Then, when the code is reviewed and tested, it will be merge on the master branch ( the production branch ).
Very useful when more than one dev is working on a project and it’s common practice to use branches instead of working directly on the original branch.
Let’s imagine I want to change my website and I want the process to be transparent for the people seeing it.
My current website code is on master branch and then if I commit and push my change, the Gitlab CI/CD pipeline will automatically deploy my new website on production.
If I have any bugs, others will see my bug immediately and it will ruin the amazing reputation of my website ! No ! I cannot afford that ! π
So i will create a new branch and work on it.
When I’m good with the change and the website is tested on testing/staging environment, i will push the changes back to the master branch to update the website.
Git branch workflow
The workflow is quite simple and easy to understand. Let’s have a look step by step on it.
Create a new GIT branch
First, of course, you need to create a branch, from GUI or CLI.
For my example, we will use the git command directly as i’m quite allergic with interface. π
Let’s imagine we have a bug on our website and we want to create a new branch.
MacBook-Air-de-Julien:resume JulienSimon$ git branch
* master
MacBook-Air-de-Julien:resume JulienSimon$ git checkout -b issue13_bugfix
Switched to a new branch 'issue13_bugfix'
MacBook-Air-de-Julien:resume JulienSimon$ git branch
* issue13_bugfix
master
This command is a combination of 2 commands as below :
# Create the branch issue13_bugfix
git branch issue13_bugfix
# Switch to the new branch
git checkout issue13_bugfix
HEAD always points to the currently checked out branch or commit.
In our case, it is the branch issue13_bugfix.
MacBook-Air-de-Julien:resume JulienSimon$ git branch
* issue13_bugfix
master
Our project is now like this on our Git local repository :
Working with Git branch
Once, you have created the branch and you switched on it with the previous command, you can start to work on your files and commit.
Each commit will be done on the new branch and you will merge your code later with the master branch.
Just imagine, i made few changes on my config files of Hugo and my website is not working anymore.
vim config.toml
git commit -m 'issue13_bugfix bad character URL'
As you see, i keep the same naming that the branch name and I make a brief explanation of the change i did, this is matter of organization as i will keep a trace of the bug and the solution.
It will be easy to see later what is the change i did to solve the issue.
Then, a new commit was created in the branch issue13_bugfix.
MacBook-Air-de-Julien:resume JulienSimon$ git log -1
commit cc598556930e1e4f4bad13646bc686d0ada73bba (HEAD -> issue13_bugfix)
Author: jusi <julsim13@yahoo.fr>
Date: Tue Sep 8 09:17:47 2020 +0200
issue13_bugfix bad character URL
Pointers always move to the latest commit in that branch that we checked out.
Modification in the issue13_bugfix branch did not affect any other branch.
Branching enables you to isolate your work from others!
As you can see, the commit is done on the new branch and no change is made on the original one.
Merge your branch with the production branch master
The git merge command will allow to select the new branch and merge with the original one, the master.
You can of course merge different others branch together before putting on the production one.
git merge will combine multiple sequences of commits into one unified history.
Preparation before merging
Before performing a merge there are a couple of preparation steps to take to ensure the merge will go smoothly and will avoid any conflict.
Commit all your change on your local branch
Before anything, commit your last changes.
git commit -m 'issue13_bugfix modify URL'
Check than your local repository is up-to-date with the master branch
Check if your local repository is up to date with the latest changes from your remote server with the git fetch command.
git fetch
Switch on the receiving branch
The target of this integration (i.e. the branch that receives changes) is always the currently checked out HEAD branch, so you need to switch on the master branch.
MacBook-Air-de-Julien:resume JulienSimon$ git branch
* issue13_bugfix
master
MacBook-Air-de-Julien:resume JulienSimon$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Ensure the master branch has the latest updates
Get the latest updates/change from master.
MacBook-Air-de-Julien:resume JulienSimon$ git pull
Already up to date.
No surprise here as I’m the only one working on this website π
Time to Git merge
Once the preparations are completed, you can start the merge with git merge command.
To use the command, be sure you are on master and specify the name of your local branch.
After the preparation and before the merge :
JulienSimon$ git merge issue13_bugfix
Updating 7ecc20e..1e30c15
Fast-forward
config.toml | 2 +-
content/posts/command-git.md | 38 --------------------------------------
content/posts/gitbranch.md | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 150 insertions(+), 39 deletions(-)
delete mode 100644 content/posts/command-git.md
create mode 100644 content/posts/gitbranch.md
After the merging of commit between branch issue13_bugfix and master :
How to Deal With Merge Conflicts
Merge conflicts occurs when one or more developers makes changes to the same line of code on the same file in 2 different branches you’re trying to merge. ( by example our new branch and master ).
Most of the time, git is pretty smart to handle the conflict on your behalf but in this case Git won’t be able to figure out which version to use and Git will need your help !
Let’s make a merge conflict ! You will never say that usually π
I will use what we learn before and create a new branch test_conflict.
MacBook-Air-de-Julien:resume JulienSimon$ git checkout -b test_conflict
M content/posts/GIT_branching.md
M content/posts/PACKER_First_Ubuntu_Template.md
Switched to a new branch 'test_conflict'
MacBook-Air-de-Julien:resume JulienSimon$ git branch
master
* test_conflict
MacBook-Air-de-Julien:resume JulienSimon$ vim content/posts/PACKER_First_Ubuntu_Template.md
MacBook-Air-de-Julien:resume JulienSimon$ git checkout master
M content/posts/GIT_branching.md
M content/posts/PACKER_First_Ubuntu_Template.md
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
MacBook-Air-de-Julien:resume JulienSimon$ vim content/posts/PACKER_First_Ubuntu_Template.md
I change the same line on the same file from 2 different branches. Git will get lost when I will want to merge my 2 branches.
And that’s it ! Now, may i need to delete my branches and is it safe ?
The commit history are merge now with the master, then you can delete the branch if you are sure than all commit are done and you will not need anymore.
You can delete branches locally by executing:
git branch -d issue13_bugfix
Deleted branch issue13_bugfix (was 1e30c15).
If it contains unmerged changes , git will tell you and won’t delete it. So, deleting a merged branch is cheap and won’t make you lose any history.
You can see which branches are commit by executing :
git branch --merged
If some of them is just abandoned stuff that you donβt need anymore, remove it with β-Dβ option instead of -d, it will force the deletion
git branch -D issue13_bugfix
That’s it, now you are ready to use branch and if you are working on collaboration, please use it !