This is a hands-on guide/tutorial for git branching and merging using the commands
git branch and
git merge. It describes how to collaborate with a team on multiple branches and merge them together. Merge conflicts will be discussed in the next article.
The guide follows a hands-on approach with examples and exercises.
Git branching allows you to collaborate with a team, have multiple “work-copies” for separate stories, features and bugs and always keep one stable and clean version of the code.
Git branches are a separate copy of the code in the repository. Multiple developers can work on different features on the same codebase. This means that two developers could have an entirely different version of the same file during the process (the merging of the branches will be discussed later on).
Let’s say we want to work on a new feature without touching the existing code. We can create a branch for that with:
git branch feat/my-new-feature git checkout feat/my-new-feature
Since this is a common use case there is even a shortcut, so we don’t have to use two commands:
git checkout -b feat/my-new-feature
This will do the same as the two commands above:
Create a branch and checking it out to work on.
Working on a branch
Working on a branch is the same as working “normally” with git. In fact if you don’t create any branches, you’ll be working on the
master branch. Some companies and devs rename the
master branch to something else like
When you’re done with the work, you can even push it to the remote and it will create a new branch with your changes on the remote repository.
Deleting a Branch
If you’re done with your work and it has been merged into a common development or the
master branch, you can delete the branch, if you won’t use it anymore.
git branch -d feat/my-new-feature
Create a new git repository and add a couple of files. Commit some changes on your master branch and create 2 new branches. Check those branches out and change the files on each one. Try deleting, creating and editing the files. Also attempt to work on the same files and lines on the different branches so you’ll have a conflict later on.
Git merging is the process of putting the changes from your branch into another branch.
It is mostly used to merge back the changes you’ve made on your branch to implement a feature or story into the development or master/main branch.
git checkout develop git merge feat/my-new-feature
To merge your changes into the develop branch, you first need to checkout the develop branch and then you can merge your branch.
merge request/ pull request
However, it is common in software development teams to not directly merge your changes into develop or master, but instead use features from GitLab or GitHub.
Merge Requests or Pull Requests (respectively) are used to look at the DIFF of your branch, review the code and add comments, suggestions and improvements, before finally merging the branch changes into develop.
The DIFF of your branch is basically everything that is different from the current version of the target branch:
Then you select to create a new MR/PR (Merge Request/Pull Request).
After that, select your target branch:
You can then add different metadata, title, description and some additional infos to your MR/PR:
After the code has been reviewed and corrected, it can be merged into the target branch:
Merge one of your branches directly with git (on your local machine) and the other one via a MR/PR.
You may encounter some conflicts. To solve these you can just ignore them or use an IDE or editor (like vs code).
It doesn’t really matter what the outcome is after the conflict, since we’re going to take a look at conflicts in the next article.