Git Branch Fundamentals
What is a Git Branch?
A Git branch is a lightweight, movable pointer to a specific commit in the repository's commit history. Branches allow developers to work on different features or experiments simultaneously without affecting the main codebase.
Branch Basics
Creating a Branch
To create a new branch in Git, you can use the following command:
git branch feature-branch
Switching Branches
To switch to a newly created branch:
git checkout feature-branch
Modern Git versions support a shorthand command:
git switch feature-branch
Branch Visualization
gitGraph
commit
branch feature-branch
checkout feature-branch
commit
commit
checkout main
commit
Branch Types
Branch Type |
Purpose |
Example |
Main Branch |
Primary development line |
main or master |
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 |
Best Practices
- Keep branches short-lived
- Use descriptive branch names
- Merge or delete branches after completion
By understanding these fundamentals, developers can effectively manage their code using Git branches in LabEx development environments.