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
HEADis 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
masterbranch and one commit as the starting point of the project.HEADpoints to themasterbranch. -
To work on a new feature, create a new branch
featurefrom themasterbranch using the commandgit branch feature. -
Commits are made to the
featurebranch 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
masterbranch that needs to be fixed. In order not to mix the bug fix with the new feature, create a new branchbug-fixfrom themasterbranch. - Checkout the
masterbranch using the commandgit checkout master. -
Create a new branch
bug-fixfrom themasterbranch and checkout using the commandgit checkout -b bug-fix. -
Commits are made to the
bug-fixbranch to fix the bug using the commandgit commit -m "message". -
Once the bug is fixed, merge the
bug-fixbranch into themasterbranch. - Checkout the
masterbranch using the commandgit checkout master. -
Merge the
bug-fixbranch into themasterbranch using the commandgit merge bug-fix. -
At this stage if your feature changes are complete, to include the new feature in the
masterbranch, merge thefeaturebranch into themasterbranch - Checkout the
masterbranch using the commandgit checkout master. -
Merge the
featurebranch into themasterbranch 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
masterbranch into thefeaturebranch. - Checkout the
featurebranch using the commandgit checkout feature. -
Merge the
masterbranch into thefeaturebranch using the commandgit merge master. This will include the bug fix changes in thefeaturebranch. -
Make further changes to the
featurebranch and commit the changes using the commandgit commit -m "message". -
Once the feature is complete, merge the
featurebranch into themasterbranch. - Checkout the
masterbranch using the commandgit checkout master. -
Merge the
featurebranch into themasterbranch using the commandgit merge feature. -
Finally, delete the
featurebranch using the commandgit branch -d feature. -
The
masterbranch 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.