keepcalculator

06 Numbers and Powers

Git Branch Command: How to Use It

Card No. 06·54763 · Updated · Standard formula, shown below

The git branch command is Git’s tool for listing, creating, renaming, and deleting branches. You run it inside a Git repository, and it shows you the branches that exist, lets you create new ones, and removes ones you no longer need.

What a Branch Actually Is

In Git, a branch is just a movable pointer to a specific commit. When you make a new commit, the branch you are on moves forward to point at that new commit. Other branches stay where they are, pointing at older commits. This lets you work on different features or fixes in parallel without interfering with each other.

Every repository has at least one branch. The default is usually called main (or master on older repositories). The special pointer HEAD tells Git which branch you are currently on. When you run git branch with no arguments, it lists all local branches and puts an asterisk (*) next to the one you are on.

Basic git branch Commands

The most common uses of git branch are:

  • git branch – list all local branches.
  • git branch <branch-name> – create a new branch at the current commit.
  • git branch -d <branch-name> – delete a branch that has been merged.
  • git branch -D <branch-name> – force delete a branch, even if it has unmerged work.
  • git branch -m <old-name> <new-name> – rename a branch.
  • git branch -a – list all branches, including remote-tracking branches.

To switch to a branch after creating it, use git switch <branch-name> (or the older git checkout <branch-name>).

Creating a New Branch

When you run git branch feature-login, Git creates a new pointer called feature-login at the same commit you are currently on. It does not switch you to that branch. You stay on your current branch. To create and switch in one step, use git switch -c feature-login or git checkout -b feature-login.

You can also create a branch from a specific commit, tag, or another branch:

git branch bugfix abc1234

This creates a branch named bugfix pointing at commit abc1234.

Deleting Branches

When a branch has been merged into your main line of development, you can delete it with git branch -d <branch-name>. Git will refuse to delete a branch that contains commits not present in the current branch, to prevent you from accidentally losing work. If you are absolutely sure you want to discard those commits, use -D to force the deletion.

Deleting a branch only removes the pointer. The commits themselves remain in the repository’s object database until Git’s garbage collection runs. If you realize you deleted the wrong branch, you can often recover it by finding the commit hash in git reflog and creating a new branch at that commit.

Renaming and Moving Branches

To rename a branch, use git branch -m <old-name> <new-name>. If you omit the old name, Git renames the current branch. For example, git branch -m main renames the current branch to main.

The -M option forces the rename even if a branch with the new name already exists. Be careful with that, as it will overwrite the existing branch pointer.

Listing Branches and Remote Branches

git branch lists only local branches. To see remote-tracking branches (branches that represent the state of branches on a remote like GitHub), use git branch -r. To see both, use git branch -a.

Remote-tracking branches are prefixed with the remote name, like origin/main. They are not the same as actual branches on the remote; they are local snapshots of what the remote looked like the last time you fetched. To update them, run git fetch.

To see more information about each branch, such as the last commit and whether it is merged, use git branch -v or git branch -vv (which also shows the upstream branch).

Common Mistakes and Pitfalls

  • Forgetting to switch after creating. git branch new-branch does not move you to it. Many beginners expect to be on the new branch immediately.
  • Deleting a branch with unmerged work. Using -D discards commits that are not in any other branch. Always double-check with git branch --merged and git branch --no-merged to see which branches have been merged.
  • Confusing git branch with git checkout. git branch only manages branches; it does not switch. Use git switch to change branches.
  • Deleting the branch you are on. Git will refuse to delete the current branch. Switch to another branch first.

Frequently Asked Questions

How do I create a branch and switch to it at the same time?

Use git switch -c <branch-name> (Git 2.23+) or git checkout -b <branch-name> for older versions. This creates the branch at the current commit and immediately moves HEAD to it.

How do I delete a branch that has already been merged?

Run git branch -d <branch-name>. Git checks that the branch is fully merged into the current branch (or its upstream) and deletes it. If it is not merged, Git will warn you and suggest using -D if you really want to lose the work.

Why does git branch not show my remote branches?

By default, git branch only lists local branches. Use git branch -r for remote-tracking branches or git branch -a for both. If you still don’t see a branch that exists on the remote, run git fetch to update your remote-tracking refs.

What is the difference between git branch and git checkout?

git branch creates, lists, renames, or deletes branches. git checkout (or git switch) changes which branch you are on. git checkout -b combines both actions: it creates a new branch and switches to it.

Can I rename the current branch?

Yes. Run git branch -m <new-name>. If you omit the old name, Git uses the current branch. To rename a different branch, specify both: git branch -m <old-name> <new-name>.

How do I recover a deleted branch?

If you deleted a branch with -D, the commits are still in the repository until garbage collection. Find the commit hash with git reflog, then create a new branch at that commit: git branch <branch-name> <commit-hash>.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →