Skip to main content

Command Palette

Search for a command to run...

Branching and Merging Decoded

Published
3 min read
Branching and Merging Decoded
I

I am passionate about technology, a pharmacist and a passionate advocate for public health. I firmly believe that leveraging technology is pivotal in advancing healthcare and ensuring its accessibility to all individuals.

When I am not immersed in the activities stated above, I find solace in indulging in life's simple pleasures. Reading captivating books, watching thought-provoking movies, and cherishing invaluable moments with loved ones are among the many ways I find joy outside of my professional pursuits.

Read, follow and leave a comment ❤️

Welcome back to our GitHub mastery series! Today is a pivotal day as we unravel the magic of branching and merging. These skills are the backbone of collaborative development, allowing teams to work seamlessly on different features without stepping on each other's toes. Let's dive into the essentials.

Understanding Branches: A Crucial Part of Parallel Development

Branches in a Nutshell:

In Git, branches are like parallel universes for your code. They enable you to work on distinct features or bug fixes without disrupting the main project. Picture it as creating alternate timelines for your codebase.

1. Understanding Branches and Their Use Cases:

Branches are not just technical nuances; they are powerful tools with real-world applications. Consider a scenario where you're developing a new feature for a web application. You wouldn't want your experimental changes to affect the stability of the existing codebase. Here, branches shine by providing isolation for your work.

Example:

# Create a new branch named "my-new-branch"
git branch my-new-branch

# Switch to the new branch
git switch my-new-branch

In this example, we've created a new branch called "my-new-branch" to work on your code. You're now free to make changes without impacting the main project.

2. Creating and Switching Branches:

Now that you understand the 'why,' let's get hands-on with the 'how.' Creating and switching branches is a fundamental skill in Git.

Example:

# Create a new branch and switch to it in one command

git switch -c my-second-branch

Here, we create and switch to a new branch named "my-second-branch" in one swift command. Now you're ready to implement without touching the main branch.

3. Merging Branches:

Eventually, you want to bring your changes back into the main project. That's where merging comes into play.

Example:

# Switch to the main branch
git switch main

# Merge the changes from the feature branch
git merge my-new-branch

Here, we switch back to the main branch and merge the changes from the "my-new-branch" branch. Now, the enhanced branch is part of the main project.

4. Resolving Merge Conflicts:

Merging isn't always a smooth sail. When changes in different branches collide, Git might face a conflict. Resolving conflicts is part art, part science.

Example:

<<<<<<< HEAD
// Your changes
=======
// Changes from the merged branch
>>>>>>> my-second-branch

Git marks conflicting sections in the code. You need to decide which changes to keep or merge them manually.

After manually resolving the conflict, you can push your code to your repository using

'git add <filepath>'

Replace <filepath> with the path to the file that has conflict

Complete your merge with

'git merge --continue'

Commit your changes with 'git commit', then finally push the changes to the main branch with 'git push origin main'

Brace for Impact: Challenge for the Day

Your mission today is to create a new branch, make changes, switch back to the main branch, and merge your work. Bonus points if you simulate a conflict and resolve it!

Understanding branching and merging is like having a superpower in the coding universe. Next time, we'll delve into collaborative work on GitHub, exploring the art of forking repositories and orchestrating pull requests. Happy coding amidst the branches! 🌳🚀

More from this blog

Untitled Publication

14 posts