What is a Git Branch
A Git branch is an independent line of development within a Git repository. Branches allow developers to work on new features, bug fixes, or experiments without affecting the main production code.
When you create a branch, Git simply creates a new pointer to a commit. From that point onward, any new commits you make will belong to that branch. This makes branching extremely lightweight and fast in Git.
Branches are widely used in collaborative development environments because they allow multiple developers to work on different parts of a project simultaneously without interfering with each other's work.
Typical examples include:
- Creating a feature branch to develop a new functionality.
- Creating a bugfix branch to resolve an issue.
- Creating a hotfix branch to patch a production problem quickly.
Once the work on a branch is complete, it can be merged back into the main branch. You can learn more about this process in our Git Merge Examples guide.
Why Git uses branches
Git branches exist to support parallel development. Instead of making all changes directly on the main branch, developers can create separate branches for different tasks.
Key advantages include:
- Safe experimentation without affecting production code
- Parallel development by multiple developers
- Easy rollback if something breaks
- Clean and organized development workflow
For example, a team may use branches such as:
feature-loginbugfix-paymenthotfix-security
Each branch represents a specific development task.
Difference between branch and commit
A commit represents a snapshot of changes in the repository at a specific point in time. For tips on writing better history, check out our Git Commit Message Guide.
A branch, on the other hand, is simply a pointer to a commit.
| Concept | Description |
|---|---|
| Commit | Snapshot of repository changes |
| Branch | Pointer that references the latest commit in a development line |
For example:
A --- B --- C (main)
\
D --- E (feature)In this diagram:
Cis the latest commit in themainbranchEis the latest commit in thefeaturebranch
Default branch in Git (main vs master)
Traditionally, Git repositories used master as the default branch name.
However, modern Git platforms such as GitHub and GitLab now use main as the default branch name.
| Branch Name | Meaning |
|---|---|
| master | Traditional default branch |
| main | Modern default branch |
Both represent the primary branch of development.
You can check the default branch using:
git branchThe active branch will appear with an asterisk *.
Git Branch Workflow Explained
Understanding the Git branch workflow is important when collaborating with other developers or managing complex projects.
Git branches allow developers to create isolated development environments inside the same repository.
A typical Git workflow looks like this:
- Clone repository using git clone
- Create a feature branch
- Make commits
- Push branch to remote repository
- Create pull request
- Merge changes into main branch
This workflow keeps the main branch stable while allowing multiple features to be developed simultaneously.
How branching works in Git internally
Internally, Git stores branches as references to commits.
When you create a branch, Git does not copy files. Instead, it simply creates a pointer to the latest commit.
For example:
A --- B --- C (main)If you create a new branch:
git branch feature-loginGit creates another pointer:
A --- B --- C (main)
\
(feature-login)Once you start committing changes in the new branch, the branch pointer moves forward.
A --- B --- C (main)
\
D --- E (feature-login)Understanding HEAD pointer in Git
In Git, the HEAD is a special pointer that indicates the currently active branch.
For example:
HEAD -> mainThis means any new commit will be added to the main branch.
You can check the current branch using:
git branchExample output:
* main
feature-loginThe asterisk indicates the branch currently referenced by HEAD.
Visualizing Git branch history
You can visualize Git branch history using:
git log --oneline --graph --allExample output:
* e9c32f3 feature-login commit
* 7ab1d12 add login UI
| * 1c84a22 bugfix commit
|/
* b3f1c92 initial commitGit Branch Command Syntax
The git branch command is used to create, list, rename, and delete branches in a Git repository.
Basic syntax:
git branch [options] [branch-name]Without any arguments, the command lists all local branches.
Basic git branch syntax
List branches:
git branchCreate new branch:
git branch feature-loginDelete branch:
git branch -d feature-loginRename branch:
git branch -m new-branch-nameImportant git branch flags and options
Some commonly used flags include:
| Option | Description |
|---|---|
-a | List all branches (local + remote) |
-r | List remote branches |
-d | Delete branch |
-D | Force delete branch |
-m | Rename branch |
-vv | Show branch tracking information |
Example:
git branch -aDisplays both local and remote branches.
Git Branch Commands Quick Reference Table
Common git branch commands cheat sheet
| Task | Command |
|---|---|
| List branches | git branch |
| List remote branches | git branch -r |
| List all branches | git branch -a |
| Create branch | git branch branch-name |
| Create and switch branch | git checkout -b branch-name |
| Switch branch | git checkout branch-name |
Frequently used git branch options
| Action | Command |
|---|---|
| Delete branch | git branch -d branch-name |
| Force delete branch | git branch -D branch-name |
| Rename branch | git branch -m new-name |
| Push branch to remote | git push origin branch-name |
| Delete remote branch | git push origin --delete branch-name |
List and Inspect Git Branches
Before creating or modifying branches, it is important to inspect the existing branches in your repository. Git provides several commands to list local branches, remote branches, and view commit information associated with each branch.
List local branches
To display all local branches in the current repository, use the following command:
git branchExample output:
* main
feature-login
bugfix-paymentThe asterisk (*) indicates the currently active branch.
List remote branches
To view branches that exist in the remote repository, use:
git branch -rExample output:
origin/main
origin/feature-login
origin/hotfix-securityRemote branches are prefixed with the remote name (commonly origin).
List all branches (local + remote)
To display both local and remote branches, run:
git branch -aExample output:
* main
feature-login
remotes/origin/main
remotes/origin/feature-loginThis command is useful when verifying whether a branch exists locally or only on the remote repository.
Show last commit of each branch
You can display the latest commit associated with each branch using:
git branch -vExample output:
* main a34f2c1 Update README
feature-login 91c4b3a Add login functionalityFor more detailed information including upstream tracking:
git branch -vvCreate a Git Branch
Create branch using git branch
To create a new branch without switching to it:
git branch feature-loginThis creates the branch but keeps you on the current branch.
Verify the branch:
git branchCreate branch and switch using git switch
Modern Git versions recommend using the git switch command to create and switch branches.
git switch -c feature-dashboardExample output:
Switched to a new branch 'feature-dashboard'Create branch using git checkout -b
An older but still widely used method:
git checkout -b feature-apiThis command:
- Creates the branch
- Switches to the new branch immediately
Create branch from another branch
Sometimes you may want to create a branch based on a different branch instead of the current one.
Example:
git checkout main
git branch feature-reportOr directly:
git switch -c feature-report mainThis creates feature-report from the latest commit of main.
Create branch from specific commit
You can create a branch from a specific commit in the repository history.
First identify the commit hash:
git log --onelineExample output:
c19e2d3 Fix login bug
8a1d3c9 Add authentication moduleCreate branch from the commit:
git branch hotfix-auth 8a1d3c9Now the new branch points to that commit.
Switch Between Git Branches
Switch branch using git switch
The recommended modern command:
git switch feature-loginExample output:
Switched to branch 'feature-login'Switch branch using git checkout
Older Git versions use the checkout command:
git checkout feature-loginThis performs the same operation but switch is preferred for clarity.
Verify current active branch
To verify the active branch:
git branchExample output:
main
* feature-login
bugfix-paymentThe branch with the * symbol is the active branch.
You can also check using:
git statusExample output:
On branch feature-loginCreate Remote Git Branch
Push local branch to remote
To push a local branch to the remote repository, you can use git push.
git push origin feature-loginThis creates a remote branch called feature-login.
Set upstream tracking branch
To link the local branch with the remote branch:
git push -u origin feature-loginAfter setting the upstream branch, you can simply run:
git pushor
git pullwithout specifying the branch name.
Verify remote branch creation
To verify that the branch exists on the remote repository:
git branch -rExample output:
origin/main
origin/feature-loginAlternatively, fetch the latest remote references:
git fetch --all
git branch -aThis confirms that the branch exists both locally and remotely.
Track Remote Branch
When collaborating with other developers, you may need to work on a branch that exists only on the remote repository. Git allows you to check out that branch locally and configure it to track the remote branch automatically.
Checkout remote branch locally
If a branch exists on the remote repository but not locally, you can create a local copy using git checkout remote branch:
git checkout -b feature-login origin/feature-loginExample output:
Branch 'feature-login' set up to track remote branch 'feature-login' from 'origin'.
Switched to a new branch 'feature-login'A more modern approach using git switch:
git switch --track origin/feature-loginThis creates a local branch that tracks the remote branch.
Set upstream tracking manually
If you already have a local branch but it does not track a remote branch, you can set tracking manually.
git branch --set-upstream-to=origin/feature-loginAlternatively:
git branch -u origin/feature-loginOnce tracking is configured, Git will automatically know which remote branch to push and pull from.
Verify tracking configuration
To check the upstream tracking status:
git branch -vvExample output:
* feature-login 91c4b3a [origin/feature-login] Add login functionality
main a34f2c1 Update READMEThe square brackets indicate the remote branch being tracked.
Rename Git Branch
Rename current branch
If you want to rename the branch you are currently working on:
git branch -m new-branch-nameExample:
git branch -m feature-authenticationThis renames the current branch.
Rename another branch
To rename a branch that is not currently checked out:
git branch -m old-branch-name new-branch-nameExample:
git branch -m feature-login feature-authenticationRename remote branch safely
Renaming a remote branch requires multiple steps.
Step 1: Rename locally
git branch -m new-branch-nameStep 2: Delete old remote branch
git push origin --delete old-branch-nameStep 3: Push new branch
git push origin new-branch-nameStep 4: Set upstream tracking
git push -u origin new-branch-nameMerge Git Branch
Merge feature branch into main
First switch to the target branch:
git switch mainThen merge the feature branch:
git merge feature-loginExample output:
Updating a34f2c1..91c4b3a
Fast-forwardFast forward vs non fast forward merge
Git can perform two types of merges.
Fast-forward merge
Occurs when the target branch has not diverged.
A --- B --- C (main)
\
D --- E (feature)After merge:
A --- B --- C --- D --- E (main)Non fast-forward merge
Occurs when both branches have independent commits.
Git creates a merge commit to combine histories.
Verify merge history
To view merge history:
git log --oneline --graph --allExample output:
* f31c7e2 Merge branch 'feature-login'
|\
| * 91c4b3a Add login functionality
|/
* a34f2c1 Update READMEDelete Git Branch
Branches should be removed after merging to keep the repository clean.
Delete local branch
To delete a local branch:
git branch -d feature-loginExample output:
Deleted branch feature-login (was 91c4b3a).Git prevents deletion if the branch contains unmerged changes.
Force delete local branch
If the branch contains unmerged commits:
git branch -D feature-loginThis forces the deletion.
Delete remote branch
To delete a branch from the remote repository:
git push origin --delete feature-loginExample output:
To github.com:user/repo.git
- [deleted] feature-loginVerify branch deletion
Check remaining branches:
git branchCheck remote branches:
git branch -rRecover Deleted Git Branch
If a branch is deleted accidentally, Git still retains commit history, allowing recovery.
Recover branch using git reflog
Git maintains a log of recent actions called the reflog, which can be inspected using git reflog.
git reflogExample output:
91c4b3a HEAD@{0}: commit: Add login functionality
a34f2c1 HEAD@{1}: checkout: moving from main to feature-loginFind the commit hash associated with the deleted branch.
Restore branch from commit history
Create a new branch from the commit:
git branch feature-login-recovered 91c4b3aSwitch to the restored branch:
git switch feature-login-recoveredYour deleted branch is now restored.
Create Branch From Stash
Git stash allows you to temporarily save changes without committing them using git stash.Git stash allows you to temporarily save changes without committing them.
Save changes using git stash
To store current changes:
git stashExample output:
Saved working directory and index state WIP on feature-loginCreate branch from stash
You can directly create a branch from the stashed changes.
git stash branch feature-tempThis command:
- Creates a new branch
- Applies the stashed changes
- Removes the stash entry
Apply stash to new branch
Alternatively, apply stash manually.
First create a branch:
git switch -c feature-tempThen apply stash:
git stash applyIf the changes are no longer needed in stash:
git stash dropThis workflow is useful when switching tasks quickly during development.
Git Branch vs Git Checkout vs Git Switch
Git provides multiple commands to work with branches, and understanding the difference between git branch, git checkout, and git switch is important for managing branches effectively.
Difference between git branch and git checkout
The git branch command is used primarily to create, list, rename, or delete branches, but it does not automatically switch to the newly created branch.
Example:
git branch feature-loginThis command creates the branch but keeps you on the current branch.
To switch to the new branch, you must run:
git checkout feature-loginAlternatively, you can create and switch in one step:
git checkout -b feature-loginSummary:
| Command | Purpose |
|---|---|
git branch | Create or manage branches |
git checkout | Switch between branches or restore files |
git checkout -b | Create and switch branch |
Why git switch was introduced
The git switch command was introduced in Git 2.23 to simplify branch switching.
Previously, git checkout handled multiple tasks such as:
- Switching branches
- Restoring files
- Creating branches
This made the command confusing for beginners.
To solve this problem, Git introduced two separate commands:
| Command | Purpose |
|---|---|
git switch | Switch between branches |
git restore | Restore files |
Example of switching branch:
git switch feature-loginCreate and switch branch:
git switch -c feature-loginThis improves command clarity and reduces mistakes.
When to use each command
The following table summarizes when to use each command.
| Task | Recommended Command |
|---|---|
| Create branch | git branch |
| Create and switch branch | git switch -c |
| Switch branches | git switch |
| Older Git versions | git checkout |
| Restore files | git restore |
For modern Git workflows, it is recommended to prefer git switch over git checkout when working with branches.
Frequently Asked Questions
1. What is a Git branch?
A Git branch is an independent line of development that allows developers to work on features, bug fixes, or experiments without affecting the main codebase. Each branch points to a commit and moves forward as new commits are added.2. How do I create a new branch in Git?
You can create a branch usinggit branch branch-name. To create and switch to the new branch immediately, use git switch -c branch-name or git checkout -b branch-name.3. How do I switch between Git branches?
To switch branches, usegit switch branch-name. Older Git versions use git checkout branch-name. The currently active branch will receive new commits.4. How do I delete a Git branch?
To delete a local branch usegit branch -d branch-name. To delete a remote branch use git push origin --delete branch-name.5. What is the difference between git branch and git checkout?
Thegit branch command is used to create and manage branches, while git checkout is used to switch branches or restore files. Modern Git versions recommend using git switch for branch switching.6. How do I see all branches in Git?
Usegit branch to list local branches. To see remote branches run git branch -r. To display both local and remote branches use git branch -a.7. How do I recover a deleted Git branch?
You can recover a deleted branch usinggit reflog to find the last commit reference and then recreate the branch using git branch branch-name <commit-hash>.Summary
Git branching is one of the most powerful features that enables developers to work on multiple lines of development within the same repository.
In this guide, we explored several important Git branch operations including:
- Understanding Git branch concepts
- Listing and inspecting branches
- Creating new branches
- Switching between branches
- Tracking remote branches
- Renaming branches
- Merging branches
- Deleting local and remote branches
- Recovering deleted branches
- Creating branches from stash
By organizing your workflow around branches, you can safely develop features, fix bugs, and collaborate with other developers without affecting the main production code. For common issues, see cannot delete checked-out branch error.
Official Documentation
You can learn more about Git branching from the official Git documentation:




![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)





