Git Branch Basics
Understanding Git Branches
Git branches are fundamental to version control, enabling developers to create independent lines of development within a repository workflow. A branch represents an isolated workspace where changes can be made without affecting the main codebase.
Core Branch Concepts
graph TD
A[Main Branch] --> B[Feature Branch 1]
A --> C[Feature Branch 2]
B --> D[Commit]
C --> E[Commit]
Branch Type |
Purpose |
Typical Usage |
Main Branch |
Primary development line |
Stable production code |
Feature Branch |
Isolated development |
New features or experiments |
Hotfix Branch |
Quick bug repairs |
Urgent production fixes |
Creating and Managing Branches
To create a new branch in Git version control, use the following command:
## Create a new branch
git branch feature-login
## Switch to the new branch
git checkout feature-login
## Alternatively, create and switch in one command
git checkout -b feature-login
Branch Demonstration on Ubuntu 22.04
## Initialize a new Git repository
mkdir git-branch-demo
cd git-branch-demo
git init
## Create and list branches
git branch feature-authentication
git branch -a
## View current branch
git branch
When working with git branches, developers can efficiently manage repository workflow by creating isolated environments for different development tasks, ensuring clean and organized code progression.