Branching
Branching is a powerful feature of Git that allows you to work on new features or bug fixes without affecting the main project code. It enables you to create isolated branches, work on them independently, and then merge them back into the main branch when ready.
Main/Master Branch
The main
or master
branch is the default branch in Git. It typically represents the main project code and is the branch that is deployed to production.
When working on new features or bug fixes, it's common practice to create separate branches from the main
or master
branch, work on them, and then merge them back into the main
or master
branch when ready.
Branching Commands
Listing Branches
To list all the branches in a repository, use the following command:
Creating a Branch
To create a new branch, use the following command:
Replace <branch-name>
with the name of the new branch.
For example, to create a branch named feature-1
, you would use the following command:
Switching Branches
To switch to a different branch, use the following command:
Replace <branch-name>
with the name of the branch you want to switch to.
For example, to switch to the feature-1
branch, you would use the following command:
Creating and Switching Branches
To create and switch to a new branch in one step, use the following command:
Replace <branch-name>
with the name of the new branch.
For example, to create and switch to a branch named feature-2
, you would use the following command:
Merging Branches
To merge changes from one branch into another, use the following command:
Replace <branch-name>
with the name of the branch you want to merge into the current branch.
For example, to merge changes from the feature-1
branch into the master
branch, you would use the following command:
Branching Workflow
HEAD
is a reference to the currently checked out commit or branch in Git. It is a pointer that points to the current branch or commit.
To See the visualization of the git branching workflow, click here and follow below sequence of events.
-
Consider a repository with a
master
branch and one commit as the starting point of the project.HEAD
points to themaster
branch. -
To work on a new feature, create a new branch
feature
from themaster
branch using the commandgit branch feature
. -
Commits are made to the
feature
branch to implement the new feature as you progress with the development using the commandgit commit -m "message"
. -
You found out that there is a bug in the
master
branch that needs to be fixed. In order not to mix the bug fix with the new feature, create a new branchbug-fix
from themaster
branch. - Checkout the
master
branch using the commandgit checkout master
. -
Create a new branch
bug-fix
from themaster
branch and checkout using the commandgit checkout -b bug-fix
. -
Commits are made to the
bug-fix
branch to fix the bug using the commandgit commit -m "message"
. -
Once the bug is fixed, merge the
bug-fix
branch into themaster
branch. - Checkout the
master
branch using the commandgit checkout master
. -
Merge the
bug-fix
branch into themaster
branch using the commandgit merge bug-fix
. -
At this stage if your feature changes are complete, to include the new feature in the
master
branch, merge thefeature
branch into themaster
branch - Checkout the
master
branch using the commandgit checkout master
. -
Merge the
feature
branch into themaster
branch using the commandgit merge feature
. -
However, lets consider a case where you need to make more changes and also require bugfix changes in your feature branch to test the changes. In this case, merge the
master
branch into thefeature
branch. - Checkout the
feature
branch using the commandgit checkout feature
. -
Merge the
master
branch into thefeature
branch using the commandgit merge master
. This will include the bug fix changes in thefeature
branch. -
Make further changes to the
feature
branch and commit the changes using the commandgit commit -m "message"
. -
Once the feature is complete, merge the
feature
branch into themaster
branch. - Checkout the
master
branch using the commandgit checkout master
. -
Merge the
feature
branch into themaster
branch using the commandgit merge feature
. -
Finally, delete the
feature
branch using the commandgit branch -d feature
. -
The
master
branch now contains the new feature and the bug fix changes.
This is a basic branching workflow in Git. It allows you to work on new features and bug fixes independently, and then merge them back into the main branch when ready.