Welcome to the fourth blog of the series! In the last, we created our first GitHub repository. Now that we have a basic idea of Git & GitHub. Let's get into more deep into Git branching and merging.
Branching and merging are two of the key concepts in Git that enable teams to collaborate on projects and manage code changes. In this blog, we'll discuss Git branching and merging, their benefits, and how to use them effectively.
What is Git Branching?
Git branching is a technique in Git that allows developers to create multiple versions of their codebase, known as branches. Each branch is essentially a snapshot of the project's codebase at a specific point in time. Developers can create branches to experiment with new features or ideas without affecting the main codebase.
Creating a Branch
To create a new branch in Git, use the git branch command followed by the name of the new branch:
$ git branch new-branch
This creates a new branch named "new-branch." You can confirm that the new branch was created by running the git branch
command without any arguments:
$ git branch
main
* new-branch
The git branch
command shows you all of the branches in your repository. The asterisk indicates the current branch.
Switching Between Branches
To switch between branches, use the git checkout
a command followed by the name of the branch:
$ git checkout new-branch
This command switches to the "new-branch" branch. You can confirm that you're on the new branch by running the git branch
command again:
$ git branch
main
* new-branch
Working on a Branch
Once you've switched to a branch, you can make changes to the codebase as you normally would. Git tracks the changes you make on each branch separately. You can commit your changes to the current branch using the git commit command:
$ git add .
$ git commit -m "Added new feature to new-branch"
Merging Branches
Once you've made changes on a branch, you may want to merge those changes into another branch. This is where Git merging comes into play.
Git merging is the process of combining the changes from one branch into another. The branch that you're merging changes into is known as the destination branch, and the branch that you're merging changes from is known as the source branch.
Basic Merge
To merge a branch into the current branch, use the git merge
a command followed by the name of the branch:
$ git checkout main
$ git merge new-branch
Merge Conflicts
In some cases, Git may not be able to automatically merge the changes from two branches. This can happen when the same lines of code are changed in different ways on each branch. When this happens, Git will pause the merge and alert you to the conflict.
To resolve a merge conflict, you'll need to manually edit the code to merge the changes. Git will mark the conflicting lines of code with special markers that look like this:
<<<<<<< HEAD
This is the code from the current branch.
=======
This is the code from the branch you're merging.
>>>>>>> new-branch
The code between <<<<<<< HEAD
and =======
is the code from the current branch, and the code between =======
and >>>>>>> new-branch
is the code from the branch you're merging. You'll need to edit the code to merge the changes and remove the conflict markers. Once you've resolved all of the conflicts, you can commit the changes to complete the merge.
Deleting a Branch
Once you've merged changes from one branch into another branch, you may no longer need the source branch. You can delete a branch using the git branch -d
command followed by the name of the branch:
$ git branch -d new-branch
This command deletes the "new-branch" branch. You can confirm that the branch was deleted by running the git branch
command again:
$ git branch
main
Benefits of Git Branching
Git branching provides several benefits for developers and teams:
-
Experimentation: Branching allows developers to experiment with new features or ideas without affecting the main codebase. This reduces the risk of breaking the code and allows developers to try out new ideas in a safe environment.
-
Isolation: Each branch is isolated from the others, which means that changes made on one branch do not affect the code on other branches. This allows multiple developers to work on the same project simultaneously without interfering with each other's work.
-
Parallel Development: Branching enables teams to work on multiple features or bug fixes simultaneously. This reduces the time required to complete a project and improves overall efficiency.
-
Versioning: Git branching provides versioning capabilities, allowing developers to keep track of changes made to the codebase over time. This makes it easier to revert changes or to compare different versions of the codebase.
You can check out my YouTube video.
Conclusion
Git branching and merging are essential concepts in Git that enable teams to collaborate on projects and manage code changes effectively. By creating multiple branches, developers can experiment with new features or ideas without affecting the main codebase. Merging changes from one branch into another allows teams to work on multiple features simultaneously and improves overall efficiency.
Thank you for reading and do let me know your thoughts in the comments!