Git & GitHub Course
Git & GitHub
/
Beginner

git branch

Definition

The command used to list, create, or delete branches.

Explain Like I'm New

The branch management tool. Typing it by itself lists all your branches and puts a little star next to the one you are currently on. Typing it with a name creates a new alternate universe.

Real World Example

Your boss tells you to build a login page. You type `git branch feature/login-page` to create a safe space to work.

Common Use Cases

  • •Seeing where you are
  • •Creating new branches
  • •Deleting old branches

Crucial Command Flags

Flag / OptionDescription
-aAll. List both remote-tracking and local branches.
-dDelete. Delete a local branch (prevents deletion if unmerged).
-DForce Delete. Delete a branch regardless of merge status.
-mMove/Rename. Rename the current branch.

Terminal Output

bash / terminal
# List all local branches (the one with the * is active) $ git branch # feature-login # * main # Create a new branch called 'dark-mode' $ git branch dark-mode # Delete a branch (safe delete) $ git branch -d old-feature # Force delete a branch (even if work will be lost) $ git branch -D broken-feature

Interview Questions

basic

  • Does creating a branch automatically switch you to that branch?

intermediate

  • How do you delete a branch safely?

Flash Cards

Question

Switch automatically?

Click to reveal answer
Answer

No. `git branch my-feature` creates it, but leaves you exactly where you are. You must use `git checkout` or `git switch` to actually jump into the new branch.

Question

Delete branch?

Click to reveal answer
Answer

`git branch -d branch-name`. Git will actually stop you and throw an error if the branch contains unmerged work. (You can force delete it with a capital `-D`).