Unboxing GIT Fundamentals: Part 3 of 10

Anand
4 min readJun 7, 2024

--

Part 3: Branching and Merging in Git

Welcome to the third part of our Git learning series! In this post, we’ll explore one of the most powerful features of Git: branching and merging. Branching allows you to work on different tasks or features in isolation, and merging helps you integrate these changes back into your main codebase. By mastering these concepts, you’ll be able to manage multiple lines of development effectively.

What are Branches and Why Use Them?

Branches in Git allow you to diverge from the main line of development and continue to work independently without affecting the main codebase. They are essential for:

  • Isolating Work: You can work on a new feature, bug fix, or experiment without disrupting the stable code.
  • Collaboration: Multiple team members can work on different features simultaneously.
  • Organizing Development: Branches help in organizing development work, making it easier to manage releases and hotfixes.

The default branch in Git is called main (formerly master), and it's where your stable code typically resides.

Creating and Managing Branches

Let’s start with some basic branching operations.

  1. Create a new branch:
git branch <branch-name>

This command creates a new branch with the specified name. For example, to create a branch called feature-x, you would run:

git branch feature-x

2. Switch to a branch:

git checkout <branch-name>

To start working on the feature-x branch, you would run:

git checkout feature-x

3. Create and switch to a new branch:

git checkout -b <branch-name>

This command creates a new branch and switches to it immediately. For example:

git checkout -b feature-y

4. List all branches:

git branch

This command lists all the branches in your repository and highlights the current branch.

5. Delete a branch:

git branch -d <branch-name>

To delete the feature-x branch, you would run:

git branch -d feature-x

Merging Branches

Merging is the process of integrating changes from one branch into another. Let’s look at how to merge branches in Git.

  1. Switch to the branch you want to merge into:
git checkout <target-branch>

Typically, you will merge changes into the main branch:

git checkout main

2. Merge the branch:

git merge <source-branch>

To merge changes from feature-x into main, you would run:

git merge feature-x

If there are no conflicts, Git will automatically merge the changes. However, if there are conflicts, you’ll need to resolve them manually.

Resolving Merge Conflicts

Merge conflicts occur when changes in the source branch conflict with changes in the target branch. Git will highlight these conflicts in the affected files, and you’ll need to resolve them before completing the merge.

  1. Open the conflicting files: Git marks the conflict areas with <<<<<<<, =======, and >>>>>>> markers.
  2. Resolve the conflicts: Edit the files to incorporate the necessary changes from both branches. Remove the conflict markers after resolving.
  3. Stage the resolved files:
git add <file-name>

4. Complete the merge:

git commit

Even though Git prompts you to enter a commit message, it’s good practice to explain the conflict resolution.

Example Workflow

Here’s a simple workflow demonstrating branching and merging:

  1. Create and switch to a new branch:
git checkout -b feature-login

2. Make changes and commit:

echo "Login feature implementation" > login.txt git add login.txt git commit -m "Implement login feature"

3. Switch back to main and merge changes:

git checkout main git merge feature-login

4. Delete the feature branch:

git branch -d feature-login

Best Practices for Branching and Merging

To make the most of branching and merging, follow these best practices:

  • Branch Naming Conventions: Use descriptive names for branches, such as feature/feature-name, bugfix/issue-number, or hotfix/version-number.
  • Frequent Merges: Regularly merge changes from the main branch into your feature branch to minimize conflicts.
  • Pull Requests: Use pull requests to review and discuss changes before merging them into the main branch.
  • Small, Focused Changes: Keep your branches small and focused on a single task to simplify merging and code reviews.

Conclusion

In this part of our Git learning series, we’ve explored the concepts of branching and merging. You’ve learned how to create and manage branches, merge changes, and resolve conflicts. These skills are crucial for effective collaboration and managing multiple lines of development in your projects.

In the next part, Unboxing GIT fundamentals: Part 4 of 10 , we’ll delve into working with remote repositories, including how to clone, fetch, pull, and push changes. Stay tuned, and happy branching!

Explore All Content of Unboxing GIT Fundamentals

  1. Part 1 of 10: Introduction to Git and Version Control
  2. Part 2 of 10: Basic Git Commands and Operations
  3. Part 3 of 10: Branching and Merging in Git
  4. Part 4 of 10: Remote Repositories in Git
  5. Part 5 of 10: Collaboration Workflows in Git
  6. Part 6 of 10: Advanced Git Commands and Techniques
  7. Part 7 of 10: Git Best Practices for Repository Management
  8. Part 8 of 10: Exploring Git Workflows — Git Flow, GitHub Flow, and GitLab Flow
  9. Part 9 of 10: Integrating Git with CI/CD Pipelines on GitHub and GitLab
  10. Part 10 of 10: Debugging and Troubleshooting Common Git Issues

About Author: An experienced IT professional worked in major IT companies for more than 20+ years as Solution Architect with core expertise on DevOps and Cloud. To get trained in DevOps from experts like Anand visit https://www.svsitsolutions.in or contact in WhatsApp

Other Interesting Blogs from the Author:

# 10 Surprising Facts About Git That You Probably Didn’t Know

# Hidden Secrets of Git : That You Never Knew

# Learning Teaser — Git in a Few Hours: A Comprehensive Training Guide

# Cinematic Experience with GIT : Lights, Camera, Action for Your Code!

--

--