Git Branch Basics
Understanding Git Branches
Git branches are fundamental to version control and collaborative software development. They allow developers to create independent lines of development, enabling parallel work on different features or bug fixes without disrupting the main codebase.
Core Concepts of Branches
Branches in Git represent a lightweight movable pointer to a specific commit. When you create a branch, Git creates a new pointer while maintaining the original branch's commit history.
gitGraph
commit
commit
branch feature
checkout feature
commit
commit
checkout main
commit
Branch Types and Usage
Branch Type |
Purpose |
Typical Use Case |
Main Branch |
Primary development line |
Stable production code |
Feature Branch |
Develop specific features |
Isolated feature development |
Hotfix Branch |
Urgent production fixes |
Immediate bug resolution |
Creating and Managing Branches
Basic branch operations in Ubuntu 22.04:
## Create a new branch
git branch feature-login
## Switch to a new branch
git checkout feature-login
## Create and switch in one command
git checkout -b feature-payment
## List all branches
git branch
## Delete a branch
git branch -d feature-login
Branch Workflow Example
Practical demonstration of branch workflow:
## Initialize a git repository
git init myproject
cd myproject
## Create initial commit
echo "Initial project setup" > README.md
git add README.md
git commit -m "Initial commit"
## Create feature branch
git checkout -b user-authentication
## Work on feature branch
echo "Authentication logic" > auth.py
git add auth.py
git commit -m "Add user authentication module"
## Return to main branch
git checkout main
Key Considerations
- Branches are lightweight and cheap in Git
- Branching supports non-linear development
- Each branch maintains its own commit history
- Merging allows integration of different branch developments