DEV Community

Victor Ogbonna
Victor Ogbonna

Posted on

Git Branching and Merging: A Simple Step-by-Step Guide for Beginners

As a beginner who’s slowly progressing through the different concepts in Git/GitHub, you will come across branching and merging which are very important aspects of version control.
Git branching is the act of creating separate lines of development within a Git repository, each branch represents a unique timeline of changes that can be worked on independently of the main codebase. While Merging, on the other hand, involves combining the changes from one branch into another. This is typically done when the changes in a branch are ready to be integrated into the main codebase. In simple terms, think of branching like making a copy of your project to try out new stuff without breaking the main project. And Merging is when you're happy with your changes, then you put them back into the main project so everyone benefits from your work.
In this guide, I’ll walk you through a step-by-step process for creating, working with, and merging branches in Git, whether you're solo or part of a team, after going through this guide you’ll be able to grasp these concepts perfectly.
Here are the steps:
1.Create a New Branch:
Use this command ( git checkout -b new_branch_name ) to create and switch to a new branch.
2.Make Changes:
Carry out the code changes within the new branch.
3.Commit the Changes:
After carrying out the changes, use this command ( git add. ) to stage them.
Then, use this command ( git commit -m “Your commit message” ) to save your changes to the new branch.
4.Switching between Branches:
Use this command ( git checkout branch_name ) to switch to branches.
5.Merge Branches:
First, switch to the main branch where you want to merge changes using ( git checkout main ).
Then, you merge the changes from the new branch to the main branch using this command ( git merge new_branch_name )
6.Resolve Conflicts (If there are any):
If Git comes across any conflicts like same lines of code in both branches, then you will have to manually fix it in your code editor, then add and commit the resolved changes. Also, other times you might want to manually check for the conflict s yourself, and you can do this by using this command ( git status ).
7.Delete a Branch (optional):
Once the changes from a branch are merged with the main branch, you can delete the branch using ( git branch -d branch_name ). It’s good practice to only delete a branch once you’re certain the changes have been successfully merged, and everything is working as it should.

Remember that only with constant practise can you be comfortable with git branching and merging, that being said, good luck to you as you make those changes.

Top comments (0)