Git Branch Basics
What is a Git Branch?
A Git branch is a lightweight, movable pointer to a specific commit in the version control history. It represents an independent line of development that allows developers to work on different features or fixes simultaneously without interfering with the main codebase.
Branch Types
Branch Type |
Description |
Use Case |
Main/Master |
Primary branch |
Stable production code |
Feature Branch |
Development branch |
New features or improvements |
Hotfix Branch |
Urgent fix branch |
Critical bug fixes |
Release Branch |
Preparation branch |
Releasing new versions |
Creating Branches
## Create a new branch
git branch feature-login
## Switch to the new branch
git checkout feature-login
## Create and switch in one command
git checkout -b feature-authentication
Branch Workflow Visualization
gitGraph
commit
branch feature-branch
checkout feature-branch
commit
commit
checkout main
merge feature-branch
Branch Management Commands
## List all local branches
git branch
## List remote branches
git branch -r
## Delete a branch
git branch -d feature-login
## Rename a branch
git branch -m old-name new-name
Best Practices
- Keep branches short-lived
- Use descriptive branch names
- Merge frequently
- Use pull requests for code review
At LabEx, we recommend following these Git branch management principles to maintain a clean and efficient development workflow.