The previous section covered the fundamentals of Git, such as installing and configuring Git, creating a repository, and making your first commit. Now, let us go over one of Git is most powerful features: Branching and merging.
What is Branching?
A branch in Git is essentially a reference to a specific commit. When working on a project, the main branch is typically called main or master, but you can create additional branches to work on features, fixes, or experiments without affecting the’main‘ branch.
Branches allow you to:
- Develop new features in isolation.
- Work with others without interfering with the stable codebase.
- Keep the main branch clean and stable.
Creating and Using Branches
1. Creating a New Branch
Assume you want to create a new feature called “new-feature“. To initiate and switch to a new branch:
git checkout -b new-feature
- The ‘checkout -b’ command creates and takes you to the ‘new-feature’ branch.
- Any changes you make to this branch will now be isolated.
To view all branches in your repository:
git branch
2. Working on a Branch
You can edit, stage, and commit on your branch in the same way that you would on the main branch. As an example:
1. Modify an existing file or create a new one.
2. Stage the file:
git add <filename>
3. Commit the changes:
git commit -m "Adding new feature implementation"
All of these changes are now in the ‘new-feature‘ branch, while your main branch remains unchanged.
3. Switching Between Branches
You can return to the main branch (or any other branch) by using:
git checkout main
You are now back on the main branch, and any changes you make will have an impact there.
Merging Branches
Once you are satisfied with the changes in your feature branch, merge it into the main branch.
1. Merging a Branch
To merge new-feature
into the main
branch:
1. First, make sure you are on the “main” branch:
git checkout main
2. Merge the new-feature
branch:
git merge new-feature
Git will merge the changes from the new-feature branch with the main branch. If there are no conflicts, Git will automatically merge.
2. Handling Merge Conflicts
Changes in different branches can occasionally conflict. For example, if the same line of code is edited in both branches, Git will not know which change to keep, resulting in a merge conflict.
When this happens:
1. Git will mark the conflict in the affected files. You will see something like this.
<<<<<<< HEAD
your changes in the main branch
=======
changes in the new-feature branch
>>>>>>> new-feature
2. You must manually edit the file to decide which changes to keep. You can keep changes from one branch or combine them.
3. Once you’ve resolved the conflict, stage the file:
git add <filename>
4. Complete the merge with:
git commit
Deleting a Branch
This removes the new-feature branch from your local repository. You can safely delete branches once they have been merged into main.
git branch -d new-feature
This removes the new-feature branch from your local repository. You can safely delete branches once they have been merged into main.
Summary of Key Commands
- Create a new branch:
git checkout -b <branch-name>
- Switch branches:
git checkout <branch-name>
- Merge a branch into
main
:git merge <branch-name>
- Delete a branch:
git branch -d <branch-name>
Conclusion
In this section, we looked at how to create, switch, and merge branches, as well as how to handle merge conflicts. Branching enables you to work on different features in parallel without disrupting the main project.
In Part 3, we will discuss how to work with remote repositories on platforms such as GitHub and Gitlab.
1 thought on “Git Tutorial Part 2: Branching and Merging with Git”