Git Branch Basics
What is a Git Branch?
A Git branch is a lightweight, movable pointer to a specific commit in the version control system. It represents an independent line of development that allows developers to work on different features or fixes simultaneously without affecting the main codebase.
Branch Concepts and Workflow
In Git, the default branch is typically called main
(or historically master
). When you create a new branch, it starts as a copy of the current branch, allowing parallel development.
gitGraph
commit
commit
branch feature-x
checkout feature-x
commit
commit
checkout main
commit
Types of Branches
Branch Type |
Purpose |
Example |
Main Branch |
Primary development line |
main |
Feature Branch |
Develop specific features |
feature/login |
Hotfix Branch |
Quick production fixes |
hotfix/security-patch |
Release Branch |
Prepare for new release |
release/v1.2.0 |
Creating Branches in Git
To create a new branch in Ubuntu, use the following commands:
## Create a new branch
git branch feature-login
## Create and switch to a new branch
git checkout -b feature-authentication
## List existing branches
git branch
Branch Naming Conventions
Best practices for branch naming:
- Use lowercase
- Separate words with hyphens
- Include type and description
- Examples:
feature/user-registration
bugfix/login-error
hotfix/security-vulnerability
Understanding Branch Pointers
Each branch is a movable pointer that updates as you make new commits. When you switch branches, Git updates your working directory to match the branch's latest commit.
LabEx Tip
For beginners learning Git, LabEx provides interactive environments to practice branch management and version control skills.