What is a Git branch?

A Git branch is a lightweight, movable pointer to a specific commit, giving developers an isolated line of development they can work on without affecting the main codebase.

Key Points: • Branches are cheap to create and delete because they're just a small file storing a commit hash, not a copy of the whole repository. • HEAD points to the currently checked-out branch, and new commits move that branch's pointer forward automatically. • Common uses include feature development, bug fixes, and experiments that can be discarded without any risk to main. • Branches are typically merged back into main (or a release branch) once their work is reviewed and complete, then deleted to keep the repository tidy. • Multiple developers can work on separate branches in parallel without interfering with each other's code.

Example: A developer runs git checkout -b feature/user-profile to start a new feature; all commits made afterward belong to that branch until it's merged back into main.

Code Example:

git branch feature/user-profile
git checkout feature/user-profile
# or in one step
git checkout -b feature/user-profile

Interview Tip: A concise interview answer is:

"A Git branch is a movable pointer to a commit that lets you develop in isolation from main. It's lightweight to create, and once the work is reviewed and merged, the branch is usually deleted to keep the repository clean."