A Git branch is an independent line of development that allows developers to work on features, bug fixes, and experiments without directly changing another branch. Once the work is complete, changes can be integrated into another branch using merging or rebasing.
- Allows developers to work on different tasks independently.
- Helps test new features without affecting other development work.
- Supports collaboration by allowing multiple developers to work on separate branches.
- Maintains a separate development history for different tasks.
Working
Git branches allow developers to manage different tasks independently while sharing the same repository and commit history. The workflow generally involves the following steps:
- The main branch usually contains the primary version of the project.
- Developers create feature branches to work on new functionality or fix bugs.
- Changes are saved as commits within the respective branches.
- Completed changes are reviewed and merged into the target branch.
- Release and hotfix branches can be used to manage software releases and urgent fixes.

Types of Branches
Git does not enforce predefined branch types. Teams use different branch naming conventions and workflows to organize development activities. Some commonly used branch types include:
- Main or Master Branch: Represents the primary development line and generally contains stable project code.
- Feature Branch: Created to develop a specific feature without directly modifying the main development branch.
- Develop Branch: Used in the Gitflow workflow to integrate completed features before preparing a release.
- Release Branch: Created to prepare a software release, allowing final testing and minor fixes before deployment.
- Hotfix Branch: Created to address urgent issues in the production version of an application.
Basic Git Branching Commands
Below are some essential Git branching commands that everyone should learn when starting to work with Git branching:
1. git branch
Displays all local branches in the repository. It can also create a new branch when a branch name is provided.
git branchTo create a new branch:
git branch feature-loginThe new branch is created, but the current branch remains unchanged.


2. git checkout and git switch
Both commands can be used to switch between existing branches.
git checkout pranjal
A modern alternative is:
git switch pranjalThe git switch command is specifically designed for switching branches, while git checkout supports additional operations.

3. git checkout -b and git switch -c
These commands create a new branch and immediately switch to it.
git checkout -b vipin
Alternatively:
git switch -c gfgBoth commands create the specified branch and make it the current branch.

4. git merge
Combines changes from another branch into the current branch. It is commonly used to integrate completed feature work into the main or develop branch.
git switch maingit merge feature-login
This command merges the changes from feature-login into main.
If Git cannot automatically combine the changes, it reports a merge conflict that must be resolved before completing the merge.
5. git rebase
Reapplies commits from the current branch on top of another branch. It helps maintain a linear commit history.
git switch feature-logingit rebase mainThis command reapplies the commits from feature-login on top of the latest commit in main.
Before rebase:
A---B---C main \ D---E feature-loginAfter rebase:
A---B---C main \ D'---E' feature-loginThe commits are recreated with new commit IDs. Avoid rebasing shared commits that other developers may already be using.

6. git branch -d
Deletes a local branch if Git determines that its changes have been merged into its upstream branch or current branch.
git branch -d vipinThis helps remove branches that are no longer needed after completing development work.

7. git branch -D
Forcefully deletes a local branch, even if it contains unmerged changes.
git branch -D gfgUse this command carefully because it can remove a branch containing work that has not been integrated elsewhere.

8. git branch -a
Displays all local and remote-tracking branches available in the repository.
git branch -aThis helps developers view branches associated with remote repositories along with their local branches.

9. git branch -m
Renames the current branch or a specified local branch.
git branch -m feature-login feature-authenticationThis renames the feature-login branch to feature-authentication.

Difference Between Git Merge and Rebase
Git provides merge and rebase operations to integrate changes from one branch into another. Both operations combine development work, but they handle commit history differently.
| Git Merge | Git Rebase |
|---|---|
| Combines changes from another branch into the current branch. | Reapplies commits on top of another branch. |
| Preserves the original branch commit history. | Creates new commits with different commit IDs. |
| May create a merge commit. | Usually produces a linear commit history. |
| Useful for preserving the history of collaborative work. | Useful for maintaining a cleaner, linear history. |
Both operations help integrate changes between branches. The choice depends on the team's collaboration practices and preferred commit history.

