Branching is a key feature that makes Git a powerful tool for version control. It allows multiple developers to work on different features or bug fixes simultaneously without interfering with each other's work. This section will cover the fundamentals of Git branching and the common workflows used in development.
Introducing Branching
In Git, a branch represents an independent line of development. Creating a new branch allows you to work on a feature or bug fix in isolation from the main codebase. Once your work is complete, you can merge the branch back into the main branch.
Example:
# Create a new branch named "feature-branch"
git branch feature-branch
# Switch to the new branch
git checkout feature-branch
The Main/Master Branch
The main or master branch is the default branch in most Git repositories. It is the primary branch where the source code of HEAD always reflects a production-ready state.
Example:
# Create a branch named "main"
git branch main
# Rename the default branch from "master" to "main"
git branch -m master main
Overview of HEAD
HEAD is a pointer that always points to the latest commit in the current branch. When you switch branches, HEAD points to the latest commit in the new branch.
Example:
# Show where HEAD is pointing
git log --oneline --decorate
# Move HEAD to the previous commit
git checkout HEAD^
Viewing Branches with Git Branch
Using the git branch
command, you can view all the branches in your repository. It lists both local and remote branches.
Example:
# List all local branches
git branch
# List all remote branches
git branch -r
# List all branches (local and remote)
git branch -a
Creating and Switching Branches
Creating a new branch and switching between branches are common tasks in Git. This allows you to work on multiple features or fixes simultaneously.
Example:
# Create and switch to a new branch
git checkout -b new-branch
# Switch to an existing branch
git checkout existing-branch
Git Checkout and Git Switch
The git checkout
command is used to switch branches or restore working tree files. The newer git switch
command is more intuitive and specifically designed for this purpose.
Example:
# Using git checkout to switch branches
git checkout branch-name
# Using git switch to switch branches
git switch branch-name
# Using git switch to create and switch to a new branch
git switch -c new-branch
Deleting and Renaming Branches
Managing branches includes deleting no longer needed branches and renaming branches for clarity.
Example:
# Delete a local branch
git branch -d branch-name
# Force delete a branch (if it has unmerged changes)
git branch -D branch-name
# Rename a branch
git branch -m old-branch-name new-branch-name
How Git Stores HEAD & Branches
Git stores branch information in the .git
directory. The HEAD
file contains a reference to the current branch, and each branch is a file in the .git/refs/heads
directory.
Example:
# View the contents of the HEAD file
cat .git/HEAD
# View the references of branches
ls .git/refs/heads/
Branching Workflow
Feature Branch Workflow
Each new feature is developed in a dedicated branch in the feature workflow. This isolates the feature work from the main branch, ensuring that the main branch remains stable.
Create a Feature Branch:
git checkout -b feature-branch
Work on the Feature:
Make changes and commit them to the feature branch.
git add . git commit -m "Implement feature"
Merge the Feature Branch into Main:
Once the feature is complete, merge it into the main branch.
git checkout main git merge feature-branch
Git Flow Workflow
Git Flow is a well-defined branching model that defines a strict branching structure. It uses different branches for feature development, releases, and hotfixes.
Feature Branches:
git checkout -b feature/feature-name
Release Branches:
git checkout -b release/release-name
Hotfix Branches:
git checkout -b hotfix/hotfix-name
Example Workflow
Start a New Feature:
git checkout -b feature/login
Finish the Feature:
git checkout main git merge feature/login
Create a Release Branch:
git checkout -b release/v1.0
Fix a Bug on Main Branch:
git checkout -b hotfix/critical-bug # Fix the bug and commit changes git checkout main git merge hotfix/critical-bug
By mastering branching and workflows in Git, you can efficiently manage parallel development, ensure code stability, and streamline collaboration. These practices are essential for maintaining a well-organized and productive development environment.