Day 11: Mastering Git Branching, Merging and More
Sep 26, 2024
•05:29 PM GMT+8
In this part of our Git journey, we will explore advanced concepts such as branching and merging. In order to maintain a smooth development workflow, it is vital to understand how to properly manage branches such as main
, release
, and feature
. As part of the course, we will also discuss .gitignore
and how to switch between branches efficiently. As a result of reading this post, we will be able to manage multiple branches and understand how to merge code changes effectively.
Tasks
Git Branching
In a typical Git workflow, it is often used multiple branches to isolate different types of work. Here is how it might structure branches:
Main/Master Branch (
main/master
):This branch represents the stable, production-ready version of your code.
Release Branch (
release
):Used to prepare code for production, allowing final fixes or tweaks.
Feature Branch (
feature
):This is where new feature or bug fixes are developed without affecting the main/master or release branches.
Creating and switching to a feature branch:
git checkout -b feature-01
git push origin feature-01
Work on feature branch, make commits, and once it is ready, prepare to merge.
Git Merging
After finishing work on a feature
branch, merge it into the release
branch. As a result, the changes can be tested and staged before being merged into the main/master
branch. There may be conflicts during the merge process, especially if changes have been made to the same lines of code in different branches. Here is how to handle that:
Merge feature
branch into release
branch:
First, switch to the release
branch
git checkout release
Then, merge it feature
branch
git merge feature-01
In the event of a conflict, Git will pause the merge and indicate which files are conflicting. The files can be edited manually to resolve these conflicts. We can use a text editor like VSCode, which provides an easy interface for resolving conflicts. VSCode highlights the conflicting sections, allowing you to choose between the changes or combine them.
Below is a comprehensive demonstration of resolving conflicts using VSCode during a merge process.
💡 After merging the feature branch into the release branch, the merging process for this type of method is similar to merging the release branch into the main branch.
Delete the feature
branch (optional):
Once merged, you can delete the feature
branch to clean up your workspace:
git branch -d feature-01
Git .gitignore
The .gitignore
file keeps unwanted files out of your Git repository, such as log files and build artifacts. The benefit of this is that it keeps your repository clean.
- Create a
.gitignore
file:
Add patterns for files or directory you want to ignore:
echo "build/" >> .gitignore
echo "*.log" >> .gitignore
- Stage and commit your
.gitignore
:
Add patterns for files or directory you want to ignore:
git add .gitignore
git commit -m "Add .gitignore to ignore build files and logs"
Git switch
The git switch
command offers a cleaner way to move between branches compared to checkout
.
- Switch to the release branch:
git switch release-01
- Create and switch to a new branch in one step:
git switch -c feature-02
Wrapping Up
We discussed Git branching with main
, release
, and feature
branches, as well as how to merge them to ensure smooth development cycles. With .gitignore
, we can manage untracked files and navigate between branches easily. In our next post, we will look at rolling back changes in Git-a powerful tool to handle errors.
Day 11: Mastering Git Branching, Merging and More
Discover how to manage main, release, and feature branches in Git, along with essential merging strategies.
For the passion of automated cloud solutions.
Subscribe to get the latest posts. I mostly write about Backend (Python/Bash), DevOps and Linux.