A Git branch is an independent line of development inside a Git repository. Branches allow developers to work on new features, bug fixes, experiments, and other changes without directly changing the main development line.
A branch is a movable pointer to a commit in a Git repository. It provides an independent development path for your work.
main
|
A → B → C
The branch name points to the latest commit in that development line.
Branches allow developers to work on different tasks without mixing changes unnecessarily.
The main development branch is commonly named main.
Some older repositories may use master.
main
|
A → B → C
The exact branch name is determined by the repository configuration and workflow.
A branch is not a complete copy of the entire repository. Git stores a branch as a reference pointing to a commit.
main → C
When a new commit is created on that branch, the branch pointer moves forward.
Use the following command to list local branches:
git branch
The current branch is normally marked with an asterisk
(*).
$ git branch
* main
feature-login
bug-fix
In this example, main is the currently checked-out
branch.
Use git branch followed by a branch name to create a
new branch.
git branch feature-login
This creates the branch but does not automatically switch to it.
You can create a branch and switch to it using:
git switch -c feature-login
The -c option tells Git to create the branch before
switching to it.
Use:
git branch --show-current
This displays the name of the current branch.
feature-login
A → B → C
↑
main
If you create a new branch at commit C, both branches initially point to the same commit.
A → B → C
↑
main
↑
feature
After switching to a branch, make changes and commit them normally.
git switch feature-login
git add .
git commit -m "Add login feature"
The new commit is added to the current branch.
C → D
/
A → B
\
E → F
Different branches can develop different commits after they diverge from a common commit.
Use git switch to move to another branch.
git switch maingit switch feature-loginGit updates the working tree to match the selected branch.
The older command for switching branches is:
git checkout feature-login
Modern Git provides git switch specifically for changing
branches, while git checkout also has other historical uses.
To list local and remote-tracking branches:
git branch -a
This can display branches such as:
* main
feature-login
remotes/origin/main
remotes/origin/feature-login
You can display branch names together with their latest commit using:
git branch -v
This is useful when you want to quickly inspect the current state of local branches.
Choose branch names that clearly describe the work.
feature-login
fix-navbar
add-payment
update-homepage
Clear names make collaborative projects easier to understand.
A feature branch is commonly used for developing a new feature.
git switch -c feature-login
You can develop and commit the feature without making those commits directly on the main branch.
A separate branch can be used to fix a problem in an application.
git switch -c fix-login-error
After completing the fix, the branch can later be merged into the appropriate development branch.
A local branch that is no longer needed can be deleted using:
git branch -d feature-login
Git normally prevents deletion with -d when it detects
that the branch contains commits that have not been merged.
The -D option forces deletion of a local branch.
git branch -D feature-login
To rename the current branch:
git branch -m new-name
For example:
git branch -m feature-auth
HEAD identifies the commit or branch that Git is currently using.
HEAD → main → C
When you switch to another branch:
HEAD → feature-login → D
HEAD now refers to the selected branch.
When you switch branches, Git updates tracked files in the working tree according to the selected branch.
git switch maingit switch feature-loginThis allows each branch to have its own committed version of the project.
Use git status to see the current branch and working
tree state.
git status
Example:
On branch feature-login
nothing to commit, working tree clean
git branch
git branch --show-current
git branch -a
git branch -v
git branch feature-login
git switch feature-login
git switch -c feature-login
git branch -d feature-login
git branch -D feature-login
git branch -m new-name
# Check current branch
git branch
# Create and switch to a feature branch
git switch -c feature-login
# Make changes
# Stage changes
git add .
# Commit changes
git commit -m "Add login feature"
# Check branches
git branch
git init
git add .
git commit -m "Initial commit"
git switch -c feature-homepage
# Make homepage changes
git add .
git commit -m "Create homepage feature"
git switch main
git branch
Here, the homepage work is developed separately from the
main branch.
Git branches provide separate development paths inside a repository. They allow developers to work on features, fixes, and experiments without directly mixing every change into the main branch.
git branch
git switch -c feature-name
git switch main
After creating and working on a branch, it can later be merged into another branch using Git's merge functionality.
git branch lists local branches.git branch feature-name creates a branch.git switch feature-name switches branches.git switch -c feature-name creates and switches to a branch.git branch --show-current shows the current branch.git branch -a lists local and remote-tracking branches.git branch -d deletes a local branch safely when Git allows it.git branch -D force deletes a local branch.git branch -m renames a branch.Question: Which command creates a new branch without switching to it?