Git Branch Basics
Understanding Git Branches in Version Control
Git branches are fundamental to effective git version control and software development workflow. A branch represents an independent line of development, allowing developers to work on different features or fixes simultaneously without interfering with the main codebase.
Core Concepts of Branches
Branches in Git are lightweight, movable pointers to specific commits. When you create a branch, Git simply creates a new pointer to the current commit you're on.
gitGraph
commit
commit
branch feature
checkout feature
commit
commit
checkout main
commit
Basic Branch Commands
Command |
Description |
Usage |
git branch |
List branches |
Shows all local branches |
git branch [name] |
Create new branch |
Creates a new branch without switching |
git checkout [branch] |
Switch branches |
Moves to specified branch |
git checkout -b [name] |
Create and switch |
Creates and immediately switches to new branch |
Practical Example on Ubuntu 22.04
## Initialize a new git repository
git init myproject
cd myproject
## Create a new feature branch
git branch feature/login
## Switch to the new branch
git checkout feature/login
## Make changes and commit
echo "Login implementation" > login.py
git add login.py
git commit -m "Implement user login"
This example demonstrates creating a branch for a specific feature, switching to it, and making changes without affecting the main branch's code.
Branches enable parallel development, isolation of work, and flexible software development workflows, making them a critical aspect of git version control.