Skip to content

Branch Management (list, rename, delete and more)


What Will We Learn?

In this guide, we will cover everything you need to know about managing branches in Git:

  • How to list local, remote, and all branches
  • How to filter and sort branches using various options
  • How to visualize your branch structure
  • How to rename branches safely (locally and remotely)
  • How to delete branches you no longer need
  • Common pitfalls and nuances to watch out for along the way

Listing Branches

Before you can manage branches effectively, you need to know how to view them. Git gives you several ways to list branches depending on what information you need.

Listing Local Branches

To see all branches that exist on your local machine, run:

git branch

This will output something like:

  feature/login
* main
  bugfix/navbar

The branch with the asterisk (*) and highlighted in green is your currently checked-out branch.

Listing Remote Branches

If you want to see branches that exist on your remote (e.g., origin), use the -r flag:

git branch -r

Example output:

  origin/HEAD -> origin/main
  origin/main
  origin/feature/login

Keep in mind these are just local references to the state of the remote branches the last time you fetched. To make sure you’re seeing the latest, run git fetch first:

git fetch --all
git branch -r

Listing All Branches

To see both local and remote branches in a single list, use the -a flag:

git branch -a

Example output:

* main
  feature/login
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/feature/login

Listing Options (–merged, –verbose, etc.)

Git provides several useful flags to filter and enrich the branch list:

  • --merged — Shows branches that have been fully merged into your current branch. Great for spotting branches that are safe to delete.

    git branch --merged
  • --no-merged — Shows branches that have not been merged yet. Useful for identifying work still in progress.

    git branch --no-merged
  • -v / --verbose — Shows the last commit on each branch, along with its short hash and commit message.

    git branch -v

    Example output:

      feature/login  a1b2c3d Add login validation
    * main           e4f5g6h Update README
  • -vv — Adds tracking information, showing which remote branch each local branch is tracking and whether it’s ahead/behind.

    git branch -vv
  • --sort — Sorts branches by a given key, such as most recently committed:

    git branch --sort=-committerdate

Visualizing Branches

Sometimes a list isn’t enough — you want to see how branches relate to each other. Git’s built-in log graph is a quick way to do this:

git log --oneline --graph --all --decorate

Example output:

* a1b2c3d (HEAD -> main) Update README
| * e4f5g6h (feature/login) Add login validation
|/
* 9f8e7d6 Initial commit

Graphical view (Zoom browser tab):

    gitGraph
   commit id: "9f8e7d6 Initial commit"
   branch feature/login
   checkout feature/login
   commit id: "e4f5g6h Add login validation"
   checkout main
   commit id: "a1b2c3d Update README"
  

For a more visual, interactive experience, tools like GitKraken, Sourcetree, or the Git Graph extension for VS Code can render your branch history as an interactive graph, making it much easier to understand complex histories with many parallel branches.

Renaming Branches

Why Rename Branches

You might need to rename a branch for several reasons:

  • Fixing a typo in the branch name
  • Aligning the name with a new naming convention (e.g., feature/login instead of login-feature)
  • Clarifying the purpose of the branch after requirements changed
  • Renaming the default branch (e.g., migrating from master to main)

Renaming Local Branches

To rename the branch you’re currently on:

git branch -m new-branch-name

To rename a branch that is not your current branch:

git branch -m old-branch-name new-branch-name

The -m flag stands for “move,” which is how Git internally treats renaming.

Renaming Remote Branches

Git doesn’t have a direct command to rename a remote branch. Instead, you rename the local branch, then push the new name and delete the old one on the remote:

# 1. Rename the local branch
git branch -m old-branch-name new-branch-name

# 2. Push the new branch to the remote
git push origin new-branch-name

# 3. Delete the old branch from the remote
git push origin --delete old-branch-name

# 4. Reset the upstream tracking for the new branch
git push origin -u new-branch-name

Important Nuances While Renaming Branches

  • Update your local tracking branch. After renaming remotely, make sure your local branch is tracking the correctly renamed remote branch using the -u flag as shown above.

  • Notify your team. If others have checked out the old branch, they’ll need to update their local references:

    git fetch origin
    git branch -m old-branch-name new-branch-name
    git branch -u origin/new-branch-name new-branch-name
    git remote prune origin
  • Open pull requests may break. Renaming a branch that has an open PR can sometimes cause issues depending on your Git hosting provider (GitHub, GitLab, etc.). Check the PR after renaming to confirm it still points to the right branch.

  • CI/CD pipelines may reference branch names. If you have automation tied to a specific branch name (e.g., deployment triggers on main), update those configurations too.

  • Renaming the default branch requires extra steps. On GitHub/GitLab, you’ll also need to update the repository’s default branch setting, and anyone with a local clone will need to run:

    git remote set-head origin -a

Deleting Branches

Why Delete Branches

Keeping your repository free of stale branches makes it easier to navigate and reduces confusion. Common reasons to delete a branch include:

  • The branch has already been merged into main and is no longer needed
  • The feature or experiment was abandoned
  • The branch was created by mistake or for temporary testing

Deleting Local Branches

To delete a local branch that has already been merged:

git branch -d branch-name

The -d flag is a “safe” delete — Git will refuse to delete the branch if it contains unmerged changes.

If you’re certain you want to delete a branch regardless of its merge status, force it with -D:

git branch -D branch-name

Deleting Remote Branches

To delete a branch on the remote repository:

git push origin --delete branch-name

Alternatively, you can use this equivalent shorthand:

git push origin :branch-name

Important Nuances While Deleting Branches

  • You can’t delete the branch you’re currently on. Switch to a different branch first (e.g., git checkout main) before deleting.

  • Deleting a remote branch doesn’t delete it locally, and vice versa. These are two separate operations — make sure you clean up both if needed.

  • Prune stale remote-tracking references. After a branch is deleted remotely by someone else, your local repo may still show it under git branch -r. Clean these up with:

    git remote prune origin

    or fetch with pruning enabled:

    git fetch --prune
  • Deleted branches aren’t gone immediately. If you deleted a branch by mistake, the commits are usually still recoverable for a while via git reflog, as long as they haven’t been garbage collected.

    git reflog
    git checkout -b recovered-branch <commit-hash>
  • Unmerged branch deletion is a one-way risk. Always double-check with git branch --no-merged before force-deleting to avoid losing work that was never merged anywhere else.

Last updated on