DEV Community

Cover image for Mastering Git: Branching and Merging – Part 2
Dipak Ahirav
Dipak Ahirav

Posted on • Updated on

Mastering Git: Branching and Merging – Part 2

πŸš€ Check Out My YouTube Channel! πŸš€

Hi everyone! If you enjoy my content here on Dev.to, please consider subscribing to my YouTube channel devDive with Dipak. I post practical full-stack development videos that complement my blog posts. Your support means a lot!

Introduction

In Part 1, we covered the fundamental concepts and commands needed to get started with Git. Now, we’ll dive deeper into one of Git's most powerful featuresβ€”branching and merging. This functionality not only enhances collaborative efforts but also significantly improves the development process.

What are Branches?

In Git, branches are effectively pointers to snapshots of your changes. When you want to add a new feature or fix a bug, you create a new branch to isolate your changes. This means you can work on new features without disturbing the main part of the project, often referred to as the master or main branch.

Creating a Branch

To create a branch, use the command:

git branch new-feature
Enter fullscreen mode Exit fullscreen mode

This command creates a new branch called new-feature. To switch to this branch and start working on it:

git checkout new-feature
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can create and switch to the branch in one command:

git checkout -b new-feature
Enter fullscreen mode Exit fullscreen mode

Making Changes and Committing

While on your new branch, any changes you make will only affect that branch. You can make changes, stage them, and commit them as follows:

git add .
git commit -m "Add new feature"
Enter fullscreen mode Exit fullscreen mode

These changes will not impact the main branch until they are explicitly merged back.

Merging Branches

Once your feature is complete and tested, you can merge it back into the main branch:

git checkout main
git merge new-feature
Enter fullscreen mode Exit fullscreen mode

This command switches you back to the main branch and then merges the changes from new-feature into main.

Resolving Conflicts

Sometimes, Git can’t automatically merge changes, resulting in conflicts. You’ll need to manually resolve these conflicts. Git will mark the files that have conflicts, and you must choose which changes to keep.

Deleting a Branch

After merging, if you no longer need the branch, you can delete it:

git branch -d new-feature
Enter fullscreen mode Exit fullscreen mode

This removes the branch locally. If you've pushed the branch to a remote repository, you might also want to delete it there:

git push origin --delete new-feature
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding branching and merging is crucial for managing larger projects and collaborating with others. In Part 3, we will explore more advanced Git features like rebasing and working with remote repositories.

Stay tuned to become a Git expert!

Series Index

Part Title Link
1 Introduction to Git: The Basics You Need to Know – Part 1 Read
2 Mastering Git: Branching and Merging – Part 2 Read
3 Advanced Git Techniques: Rebasing and Working with Remotes – Part 3 Read
4 Git Mastery: Tags and Releases – Part 4 Read
5 Streamlining Your Workflow with Git Hooks and Automation – Part 5 Read
6 Mastering Git: Conflict Resolution Strategies – Part 6 Read
7 Advanced Git Workflows for Efficient Project Management – Part 7 Read
8 Integrating Git with CI/CD Pipelines – Part 8 Read

Top comments (1)

Collapse
 
dipakahirav profile image
Dipak Ahirav

Next part -> Part - 3