Git Branch Fundamentals
What is a Git Branch?
A Git branch is a lightweight, movable pointer to a specific commit in the repository's history. It allows developers to create independent lines of development, enabling parallel work on different features or experiments without affecting the main codebase.
Branch Creation and Management
Creating a New Branch
To create a new branch in Git, use the following command:
git branch new-feature
To create and immediately switch to the new branch:
git checkout -b new-feature
Branch Visualization
gitGraph
commit
branch develop
checkout develop
commit
commit
checkout main
commit
Branch Types
Branch Type |
Purpose |
Typical Usage |
Main/Master |
Primary development branch |
Stable production code |
Feature |
Developing specific features |
New functionality |
Hotfix |
Fixing critical production issues |
Urgent bug repairs |
Release |
Preparing for a new release |
Version preparation |
Branch Operations
Switching Branches
git checkout target-branch
Listing Branches
## List local branches
git branch
## List all branches (local and remote)
git branch -a
Merging Branches
## Switch to target branch
git checkout main
## Merge another branch
git merge feature-branch
Best Practices
- Keep branches short-lived
- Use descriptive branch names
- Regularly sync with main branch
- Delete merged branches
LabEx Tip
When learning Git branching, LabEx provides interactive environments to practice these fundamental concepts safely and effectively.