Skip to Content
DevopsGitGit WorkflowBranching

Git Branch Commands

Git branches allow developers to work on different features or fixes without affecting the main project. Below are the most common commands used to manage branches.

List Existing Branches

Terminal
git branch

Displays the list of branches that have been created in the repository. The current branch is marked with *.

Example:

* main feature-login bugfix-header

Create a New Branch

Terminal
git branch branchA

Creates a new branch called branchA if it does not already exist.

Git Branch

Switch to a Branch

Terminal
git checkout branchA

Moves the working directory to the branchA branch if it exists.

Git Branch checkout

Create and Switch to a Branch

Terminal
git checkout -b branchA

Creates the branch branchA and immediately switches to it.

Git checkout -b

Create or Reset a Branch

Terminal
git checkout -B branchA

Creates the branch branchA if it does not exist. If it already exists, Git resets the branch and switches to it.

Rename a Branch

If you made a mistake when naming a branch:

Terminal
git branch brachA # bad name # You can rename it with: git branch -m brachA branchA

This command renames the branch from brachA to branchA.

Delete a Branch

Terminal
git branch -d branchA

Deletes the branch branchA if it has already been merged.

Git will delete the branch because its changes already exist in another branch.

Git delete a branch

Force Delete a Branch

Terminal
git branch -D branchA

Forces the deletion of the branch even if it contains unmerged changes.

Be careful : Unmerged commits will be lost.

Git delete a branch

Summary


CommandDescription
git branchList all branches
git branch nameCreate a new branch
git checkout nameSwitch to a branch
git checkout -b nameCreate and switch to a new branch
git checkout -B nameCreate or reset a branch and switch to it
git branch -m old-name new-nameRename a branch
git branch -d nameDelete a branch (only if merged)
git branch -D nameForce delete a branch
Last updated on