Git Branch Basics
What is a Git Branch?
A Git branch is a lightweight, movable pointer to a specific commit in the repository's history. It represents an independent line of development, allowing developers to work on different features or fixes simultaneously without interfering with each other.
Branch Structure and Workflow
gitGraph
commit
branch develop
checkout develop
commit
commit
branch feature-login
checkout feature-login
commit
checkout develop
merge feature-login
Key Branch Concepts
Concept |
Description |
Master/Main Branch |
The primary branch representing the stable production code |
Feature Branch |
A branch created to develop a specific feature |
Hotfix Branch |
A branch used to quickly patch production issues |
Creating Branches in Git
To create a new branch in Git, you can use the following commands:
## Create a new branch
git branch new-feature
## Create and switch to a new branch
git checkout -b bug-fix
## List all local branches
git branch
## List all remote and local branches
git branch -a
Branch Naming Conventions
Best practices for branch naming include:
- Use lowercase letters
- Separate words with hyphens
- Include type of work (feature/, bugfix/, hotfix/)
- Be descriptive but concise
Understanding HEAD and Branch Pointers
The HEAD is a special pointer that references the current branch or commit you're working on. When you switch branches, Git updates the HEAD to point to the new branch.
LabEx Tip
At LabEx, we recommend practicing branch management skills through hands-on exercises to build muscle memory and confidence in Git workflows.