What is a Branch in Git?
Learn what a Git branch is, how it works as a lightweight pointer to a commit, and how to create, switch, and merge branches in real workflows.
Expected Interview Answer
A Git branch is a lightweight, movable pointer to a specific commit, letting you develop features, fixes, or experiments in isolation without affecting the main line of history.
Internally, a branch is nothing more than a 41-byte file in `.git/refs/heads/` containing the SHA-1 of its tip commit, which is why creating one with `git branch` or `git switch -c` is nearly instant regardless of repository size. When you commit while on a branch, Git creates a new commit whose parent is the previous tip, then moves the branch pointer forward to it; the commit's history itself never physically belongs to the branch, only the pointer does. This design makes branching and switching cheap, which is what enables workflows like feature branches, trunk-based development, and short-lived topic branches for every pull request. Merging later combines the diverging histories of two branches back together, either with a merge commit or, when possible, a fast-forward.
- Isolates work-in-progress from the stable main line
- Nearly instant to create, switch, and delete
- Enables parallel development by multiple contributors
- Supports safe experimentation with easy rollback
- Underpins pull request and code review workflows
AI Mentor Explanation
A branch is like a net practice session set up alongside the main match, where a batter can try a new technique without it affecting the actual scoreboard. If the technique works, the coach brings it into the main game plan; if not, the net session is simply abandoned with no impact on the real match record.
Step-by-Step Explanation
Step 1
Create the branch
`git branch feature-x` or `git switch -c feature-x` creates a new pointer at your current commit, instantly, without copying any files.
Step 2
Switch onto it
`git switch feature-x` (or the older `git checkout feature-x`) moves HEAD to follow the new branch, updating the working directory to match its tip commit.
Step 3
Commit in isolation
Every commit you make advances the current branch's pointer forward; commits on `main` are unaffected until you merge.
Step 4
Push to share it
`git push -u origin feature-x` publishes the branch to the remote so others can see and collaborate on the same work.
Step 5
Merge or rebase back
Once the work is ready, merge it into `main` with `git merge feature-x`, or rebase it for a linear history, then delete the now-unneeded branch.
What Interviewer Expects
- Describes a branch as a lightweight, movable pointer to a commit
- Knows branch creation and switching are cheap, near-instant operations
- Understands commits don't 'belong' to a branch physically, only the pointer moves
- Can explain a typical feature-branch workflow
- Knows the difference between merging and deleting a branch
Common Mistakes
- Thinking a branch is a full copy of the codebase
- Believing branching is expensive or slow in Git
- Confusing local branches with their remote-tracking counterparts
- Forgetting to delete merged branches, cluttering the repository
- Not realizing HEAD can be detached from any branch
Best Answer (HR Friendly)
“A branch lets a developer work on a new feature or fix in a separate, safe space without disturbing the main, stable version of the project. Once the work is tested and approved, it gets merged back into the main branch, which is how teams build software in parallel without conflicts.”
Code Example
$ git switch -c feature/login-page
Switched to a new branch 'feature/login-page'
$ git add . && git commit -m "Add login page"
[feature/login-page 4b8d2c1] Add login page
$ git switch main
$ git merge feature/login-page
Updating 3f2a1b9..4b8d2c1
Fast-forward
src/pages/login.tsx | 40 ++++++++++++++++++++++++++++++++++
$ git branch -d feature/login-page
Deleted branch feature/login-page (was 4b8d2c1).Follow-up Questions
- What actually happens internally when you create a branch?
- What is the difference between a fast-forward merge and a three-way merge?
- What is a detached HEAD state?
- How do you delete a remote branch versus a local branch?
- What is trunk-based development and how does it change branching strategy?
MCQ Practice
1. What is a Git branch internally?
A branch is just a reference file storing the SHA-1 of its tip commit, which is why creating one is nearly instant.
2. What happens when you commit while on a branch?
Committing creates a new commit object and advances the current branch's pointer to reference it; other branches are unaffected.
3. Which command creates and switches to a new branch in one step (modern Git)?
`git switch -c` creates a new branch and checks it out in a single command; `git checkout -b` does the same in older Git.
Flash Cards
What is a Git branch, technically? — A lightweight, movable pointer to a specific commit — not a copy of the codebase.
Why is branching cheap in Git? — Creating a branch just writes a small reference file with a commit hash; no files are copied.
What moves when you commit on a branch? — The branch pointer advances to the new commit; other branches stay where they were.
How do you switch to a branch in modern Git? — `git switch <branch>` (the modern replacement for `git checkout <branch>`).