What is a Branch?

A branch in Git is a lightweight, movable pointer to a specific commit. It allows developers to work independently on a new feature, bug fix, or experiment without affecting the main codebase.

Think of a branch like making a copy of a Google Doc—you can make changes freely without impacting the original document. Once your work is complete and tested, you merge it back into the main branch.


Advertisement

Interview Answer

"A branch in Git is a lightweight pointer to a commit that allows developers to work independently on features, bug fixes, or experiments without affecting the main branch. Once the work is complete and tested, the branch is merged back into the main codebase."


Why Use Branches?

Branches allow developers to:

  • Develop new features independently.
  • Fix bugs without affecting production code.
  • Work on multiple tasks simultaneously.
  • Support parallel development.
  • Isolate experimental changes.

Examples:

  • feature/login
  • bugfix/payment-error
  • release/1.2.0

Git Branch Workflow

Main Branch
      │
      ├───────────────┐
      │               │
      ▼               ▼
Feature A        Feature B
      │               │
      └──────┬────────┘
             ▼
         Merge Back

Common Branch Commands

Action Command
List branches git branch
Create branch git branch feature/login
Switch branch git checkout feature/login
Create and switch git checkout -b feature/login
Delete branch git branch -d feature/login
Merge branch git checkout main
git merge feature/login
Push branch git push origin feature/login

Real-Time Example

In my project, every new feature or bug fix is developed in its own branch, such as feature/login-form. This keeps the main branch stable while allowing multiple developers to work in parallel.


Creating a New Branch

Creating a branch allows you to start new work without affecting the main branch.


Interview Answer

"Whenever I start a new feature or bug fix, I create a dedicated branch using git checkout -b or git switch -c. This keeps my changes isolated from the main branch until they are tested and reviewed."


Create a Branch

git branch branch-name

This creates a branch but does not switch to it.


Create and Switch

git checkout -b branch-name

or

git switch -c branch-name

Both commands:

  • Create the branch.
  • Switch to it immediately.

Example

git checkout -b feature/login-form

Push the Branch

git push origin feature/login-form

Best Practices

Always create branches from:

  • main
  • develop

Use meaningful names such as:

  • feature/login-page
  • bugfix/payment-error
  • release/2.0.0

Workflow

Main
 │
 ▼
Create Branch
 │
 ▼
Develop Feature
 │
 ▼
Push Branch

Switching Branches (git checkout and git switch)

Git provides two commands for switching branches.


Interview Answer

"Before switching branches, I ensure all my changes are committed or stashed. I use git switch for branch switching because it's clearer and safer, although git checkout is still commonly used."


Using git checkout

git checkout branch-name

Using git switch

git switch branch-name

Why git switch?

git checkout performs multiple tasks:

  • Switch branches
  • Restore files
  • Checkout commits

git switch focuses only on branch switching, making it easier to understand.


Comparison

Command Purpose
git checkout Switch branches, restore files, checkout commits
git switch Switch branches only

Best Practice

Before switching branches:

  • Commit your work.
  • Or stash unfinished changes.

git merge vs git rebase

Both commands combine changes from one branch into another, but they manage history differently.


Interview Answer

"I use git merge when working on shared branches because it preserves branch history. I use git rebase on my local feature branches to maintain a clean, linear commit history before creating a Pull Request."


git merge

Merge combines two branches by creating a merge commit.

git checkout main

git merge feature/login

git rebase

Rebase replays commits on top of another branch.

git switch feature/login
git rebase main

The golden rule of rebasing: rebase only your own branch that nobody else has pulled. Rebasing rewrites commit IDs, so rebasing a shared branch (like main) forces everyone else to reconcile a history that changed under them. If you must update an already-pushed branch after a rebase, use git push --force-with-lease, which refuses to overwrite commits you haven't seen.


Comparison

Feature git merge git rebase
Creates Merge Commit ✔ Yes ✘ No
Preserves History ✔ Yes ✘ No
Linear History ✘ No ✔ Yes
Rewrites History ✘ No ✔ Yes
Best For Shared branches Local feature branches

Visual Comparison

Merge

A──B──C────M
     \    /
      D──E

Rebase

A──B──C──D'──E'

Best Practices

Use:

  • Merge → Shared branches.
  • Rebase → Local feature branches.

Never rebase commits that have already been shared.


What is a Merge Conflict?

A merge conflict occurs when Git cannot automatically merge changes because two branches modify the same part of a file differently.

Git pauses the merge and asks you to resolve the conflict manually.


Interview Answer

"A merge conflict occurs when two branches modify the same section of a file and Git cannot determine which version should be kept. I resolve conflicts manually, verify the changes, stage the resolved files, and complete the merge."


Why Does It Happen?

Common reasons:

  • Two developers edit the same line.
  • One branch deletes a file while another edits it.
  • Simultaneous changes to shared files.

Conflict Markers

Git inserts markers like:

<<<<<<< HEAD

Current Branch

=======

Incoming Branch

>>>>>>> feature/login

Tips to Avoid Merge Conflicts

  • Pull frequently.
  • Keep branches updated.
  • Make small commits.
  • Communicate with teammates.
  • Avoid long-lived branches.

Resolving Merge Conflicts

Resolving conflicts involves reviewing the conflicting code and choosing the correct version.


Interview Answer

"When a merge conflict occurs, I use git status to identify the conflicting files, manually resolve the differences, remove the conflict markers, stage the resolved files using git add, and then complete the merge."


Step 1: Check Status

git status

Step 2: Open the File

Review the conflict markers.

Edit the file to keep the correct code.

Remove:

  • <<<<<<<
  • =======
  • >>>>>>>

Step 3: Stage the File

git add <file-name>

Step 4: Complete the Merge

git commit

or

git merge --continue

Conflict Resolution Workflow

Merge
   │
   ▼
Conflict
   │
   ▼
git status
   │
   ▼
Edit File
   │
   ▼
git add
   │
   ▼
Commit

Real-Time Example

During a sprint, two developers modified the same login validation class. Git reported a merge conflict. I reviewed both implementations, combined the required logic, removed the conflict markers, staged the file, and completed the merge successfully.


Deleting a Branch (Local and Remote)

After a feature is merged, the branch can be removed.


Interview Answer

"After successfully merging a feature branch, I delete the local branch using git branch -d and remove the remote branch using git push origin --delete to keep the repository clean."


Delete Local Branch

Safe deletion:

git branch -d feature/login

Force Delete

git branch -D feature/login

Use only when the branch contains unmerged commits.


Delete Remote Branch

git push origin --delete feature/login

Comparison

Command Purpose
git branch -d Delete merged branch
git branch -D Force delete branch
git push origin --delete Delete remote branch

Fast-Forward vs Three-Way Merge

Git performs different merge strategies depending on branch history.


Fast-Forward Merge

A fast-forward merge occurs when the target branch has no new commits.

Git simply moves the branch pointer forward.

No merge commit is created.


Example

Main

A──B

Feature

A──B──C

After merge:

A──B──C

Three-Way Merge

A three-way merge occurs when both branches contain new commits.

Git creates a merge commit.


Example

      C
     /
A──B
     \
      D

After merge:

      C
     / \
A──B───M
     \
      D

Comparison

Feature Fast-Forward Three-Way Merge
Merge Commit ✘ No ✔ Yes
Linear History ✔ Yes ✘ No
Branches Diverged ✘ No ✔ Yes

Benefits

Fast-Forward

  • Cleaner history
  • No merge commits
  • Simple workflow

Three-Way Merge

  • Preserves branch history
  • Better collaboration
  • Shows complete development history

Try It: A Real Merge Conflict in a Page Object

A classic automation conflict: on a feature branch, one tester switched the login button locator to a data-testid; meanwhile someone changed the same line on main. This is the real output:

$ git merge feature/new-locator
Auto-merging LoginPage.java
CONFLICT (content): Merge conflict in LoginPage.java
Automatic merge failed; fix conflicts and then commit the result.

$ git status --short
UU LoginPage.java                          # UU = both sides changed it, unmerged

$ cat LoginPage.java
public class LoginPage {
<<<<<<< HEAD
    By loginBtn = By.id("login-btn");
=======
    By loginBtn = By.cssSelector("[data-testid=login]");
>>>>>>> feature/new-locator
}

Git can't know which locator is right — that's a decision for the people involved. Here the data-testid locator is more stable, so we kept it, removed the markers, staged and committed:

$ git add LoginPage.java
$ git commit --no-edit
$ git log --oneline --graph
*   Merge branch 'feature/new-locator'
|\
| * Use data-testid for login button
* | Rename login button id
|/
* Add login page
  • Run the tests after resolving — a conflict resolution that compiles can still pick the wrong locator.
  • Conflicts in page objects and test data files are the most common in automation repositories; small, frequent merges from main keep them small.
  • To give up and start again: git merge --abort (or git rebase --abort).

Practice branching and merging questions under interview conditions in the Git Interview Simulator. More: Essential Git Commands.

From Real Projects

In my automation roles I maintained test scripts that several people reviewed and extended — POM classes, the business library and data files all changing at once. That's exactly the situation version control is built for: every change reviewed, traceable and easy to undo. On Canolog, the many forms and screens across sales, inventory, finance and service are where keeping page details in POM classes and reusable steps in a business library paid off. Merge from main often to keep conflicts small.


📚 Official documentation: Git documentation

Frequently Asked Questions (FAQs)

1. What is a branch in Git?

A branch is a lightweight pointer to a specific commit that allows developers to work independently on features, bug fixes, or experiments without affecting the main codebase. Once complete, the branch can be merged back into the main branch.


2. How do you create a new branch?

Create a branch:

git branch <branch-name>

Create and switch:

git checkout -b <branch-name>

or

git switch -c <branch-name>

3. What is the difference between git checkout and git switch?

  • git checkout is a multi-purpose command used for switching branches, restoring files, and checking out commits.
  • git switch is dedicated only to switching branches, making it simpler and safer.

4. What is the difference between git merge and git rebase?

  • git merge combines branches by creating a merge commit and preserving the complete branch history.
  • git rebase reapplies commits on top of another branch, creating a linear history while rewriting commit history.

Use merge for shared branches and rebase for cleaning up local feature branches.


5. What is a merge conflict and how do you resolve it?

A merge conflict occurs when Git cannot automatically merge changes because two branches modify the same section of a file differently.

To resolve it:

  1. Run git status.
  2. Open the conflicting files.
  3. Resolve the conflicts manually.
  4. Remove the conflict markers.
  5. Stage the resolved files using git add.
  6. Complete the merge with git commit or git merge --continue.

6. How do you delete a branch locally and remotely?

Delete a local branch:

git branch -d <branch-name>

Force delete:

git branch -D <branch-name>

Delete a remote branch:

git push origin --delete <branch-name>

7. What is a Fast-Forward Merge?

A Fast-Forward Merge occurs when the target branch has no new commits since the feature branch was created.

Instead of creating a merge commit, Git simply moves the branch pointer forward to the latest commit, resulting in a clean and linear commit history.