
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git - Branching and Merging
Branching and merging are two of the most powerful and essential features in Git, a distributed version control system used by developers to manage and collaborate on projects.
What is Branching in Git?
Branching in Git refers to the process of creating a new line of development that diverges from the main project. Each branch represents an independent set of changes that can be worked on in isolation.
By default, a new Git repository starts with a single branch called main or master. As development progresses, you can create additional branches to work on new features, bug fixes, or experimental changes without affecting the stability of the main branch.
Assume we are in charge of a software project and that we have been committing changes to the master branch, which is the main development branch.
We now have a specific feature or bug to work on, like implementing a new authentication module.

Creating and Managing Branches
Using Git, we create a new branch and switch to it all at once to isolate this work until it's ready.
$ git branch auth-module $ git checkout auth-module

Rather than creating a branch separately and then moving to it, the >-b option will simplify this for us:
$ git checkout -b auth-module
This command copies the working directory to the auth-module branch and builds it instantaneously. Git sends a message confirming this modification:
Switched to a new branch 'auth-module'
The modifications and commits will now only impact the auth-module branch.
For example, let us edit files pertaining to authentication and commit our modifications using below commands:
$ vim auth.py $ git commit -a -m 'Implement user authentication module'
The auth-module branch will separate from the master branch.

By using this method, we can work on new features or repairs in parallel while maintaining the stability of the main branch.
Let's say a bug has been reported on the live website that needs to be fixed right away (Urgent Issue Resolution).
We return to the master branch using git checkout in order to address the urgent issue.
$ git checkout master
Git ensures our working directory reflects the state of the master branch as it was at the last commit.
Git branch switching lets us concentrate on resolving the immediate problem without deploying incomplete modifications from the auth-module branch.
This division guarantees that our work on the auth-module will continue to be separate and undisturbed until it is prepared for integration.

It is vital to have a clean working directory before transferring branches. To prevent conflicts, make sure that all modifications pertaining to the auth-module are committed or stored.
Git's branch management enhances workflow efficiency by enabling rapid context switches for tasks and urgent needs, reducing effort in managing and implementing changes, fostering teamwork, and promptly resolving issues.
Then there's this bug that requires our immediate attention. Let's deal with this problem by creating a bugfix branch:
$ git checkout -b bugfix Switched to a new branch 'bugfix' $ vim index.html $ git commit -a -m 'Fix critical bug in login process' [bugfix 1fb7853] Fix critical bug in login process 1 file changed, 2 insertions(+)
It's crucial to do tests to make sure the patch satisfies criteria after making the required adjustments. After verification, we deploy the bugfix branch by merging it back into master:
$ git checkout master $ git merge bugfix Updating f42c576..3a0874c Fast-forward index.html | 2 ++ 1 file changed, 2 insertions(+)

We see a fast-forward notification throughout this merge. This happens as a result of the bugfix branch being based immediately before the master commit C2.
Git merely advances the reference to the master branch to incorporate the bugfix modifications.

Now that the change has been added to the master branch, we may go on to the solution's deployment.
Once the major bug patch has been deployed successfully, we can safely delete the bugfix branch from our repository since it is no longer required, keeping it organized:
$ git branch -d bugfix Deleted branch bugfix (3a0874c).
We may now go back to our auth-module branch and continue working in that branch:
$ git checkout auth-module Switched to branch 'auth-module' $ vim auth.py $ git commit -a -m 'Finish implementing user authentication module' [auth-module ad82d7a] Finish implementing user authentication module 1 file changed, 1 insertion(+)
It's crucial to understand that the auth-module branch does not always instantly incorporate the modifications made in the bugfix branch.
If required, we can use git merge master to include these modifications into auth-module.
Alternatively, we can wait to integrate auth-module into master until we are ready.
Basic Merging
When the auth-module branch is ready to be integrated into master after we've finished working on it, we start the merging process:
$ git checkout master Switched to branch 'master' $ git merge auth-module Merge made by the 'recursive' strategy. auth.py | 1 + 1 file changed, 1 insertion(+)
The previous bugfix merge is not the same as this merging.

Our development histories have varied in this instance.
Git combines changes from the master, auth-module, and their common grandparent in a three-way merge.

Git automatically creates a new snapshot as a result of this merge and generates a merge commit that references both parent branches, rather than just pushing the branch pointer forward.
Due to its consolidation of disparate histories, this merging commit is unique.
Now that the auth-module modifications have been integrated into master, we can delete the branch and close the related issue in our tracking system:
$ git branch -d auth-module Deleted branch auth-module (3a0874c).
By ensuring that finished features are merged into the master development branch, this procedure preserves the integrity and advancement of the project.