Back to Git: Branching, Merge, Rebase
Q5Question 5 of 7

What are Git fundamentals: commits, branches, and merges

Answer: Git is a distributed version control system where every developer has a full copy of the repository, and commits, branches, merges, and pull requests are the core operations that enable parallel development and safe collaboration.

Key Concepts
  • A commit is a snapshot of every tracked file at a point in time, identified by a SHA-1 hash.
  • A branch is a movable pointer to a commit, allowing parallel development without affecting the main line.
  • Merging combines changes from one branch into another, creating a merge commit that has two parents.
  • Rebasing replays commits on top of another branch, producing a linear history without merge commits.
  • A pull request is a code review mechanism where a developer proposes merging their branch into the target branch.
  • Git flow, trunk-based development, and GitHub flow are common branching strategies for teams.
  • Resolving merge conflicts requires understanding both changes and choosing the correct combined result.

Definition

Git is a distributed version control system where every developer has a full copy of the repository, and commits, branches, merges, and pull requests are the core operations that enable parallel development and safe collaboration.
  • Commit is a snapshot of all tracked files with a unique SHA-1 hash, an author, a timestamp, and a message describing the change.
  • Branch is a lightweight, movable pointer to a commit that allows you to work on a feature without changing the main codebase.
  • Merge combines the history of two branches by creating a new commit with two parent commits.
  • Rebase moves a branch's commits on top of another branch, rewriting history to produce a linear sequence.
  • Pull request (PR) is a proposal to merge a branch into the target branch, enabling code review, discussion, and CI checks before merging.

Think of the National Bank of India's policy manual. The main branch is the official version of the manual. When Rajesh wants to update the interest rate policy, he does not edit the official manual directly. He takes a photocopy (creates a branch), makes changes on his copy (commits), and submits the updated pages to the review committee (pull request). The committee reviews, suggests edits, and once approved, the changes are stapled into the official manual (merge). If Priya was updating the loan policy at the same time on her own copy, both updates happen independently and get merged into the official manual separately.

Understanding the Concepts

What is a commit?

A commit is a snapshot of every tracked file in your repository at a specific point in time. Each commit has a unique SHA-1 hash (like a1b2c3d), the author's name and email, a timestamp, and a commit message.

Commits form a linked list. Each commit points to its parent, creating a history chain.

BASH
# Initialize a repository for the banking project
mkdir national-bank-api && cd national-bank-api
git init

# Create the first file
echo 'public class AccountService {}' > AccountService.java
git add AccountService.java
git commit -m "feat: add empty AccountService class"

# Make a change
echo 'public class AccountService {
    public String getBalance(String accountId) {
        return "75000.00";
    }
}' > AccountService.java
git add AccountService.java
git commit -m "feat: add getBalance method for SAV2001"

# View history
git log --oneline
TEXT
b3f7a21 feat: add getBalance method for SAV2001
a1c9e04 feat: add empty AccountService class

Each commit is immutable. Once created, you cannot change it. You can create new commits that replace old ones (amend, rebase), but the original commits still exist in Git's object database until garbage collected.

JAVA
package com.bank.service;

public class AccountService {

    // Version tracked by Git: each change to this file
    // creates a new commit in the repository history
    public String getBalance(String accountId) {
        if ("SAV2001".equals(accountId)) {
            return "75000.00";
        }
        return "0.00";
    }

    public static void main(String[] args) {
        AccountService service = new AccountService();
        System.out.println("SAV2001 balance: " + service.getBalance("SAV2001"));
    }
}
TEXT
SAV2001 balance: 75000.00

What is a branch?

A branch is a movable pointer to a commit. When you create a branch, Git creates a pointer. When you make a new commit on that branch, the pointer moves forward to the new commit.

The main branch (or master in older repositories) is the default branch. Feature branches are created off main for new work.

BASH
# You are on main branch
git branch
# * main

# Create a feature branch for the transfer feature
git checkout -b feature/fund-transfer

# Make changes on the feature branch
echo 'public class TransferService {
    public void transfer(String from, String to, String amount) {
        System.out.println("Transferring " + amount + " from " + from + " to " + to);
    }
}' > TransferService.java
git add TransferService.java
git commit -m "feat: add TransferService with basic transfer method"

# View branches
git branch
TEXT
* feature/fund-transfer
  main
JAVA
package com.bank.service;

import java.math.BigDecimal;

public class TransferService {

    public void transfer(String from, String to, BigDecimal amount) {
        System.out.println("Transferring " + amount + " from " + from + " to " + to);
    }

    public static void main(String[] args) {
        TransferService service = new TransferService();
        service.transfer("SAV2001", "SAV3002", new BigDecimal("25000.00"));
    }
}
TEXT
Transferring 25000.00 from SAV2001 to SAV3002

How does merging work?

Merging combines the changes from one branch into another. Git creates a merge commit that has two parents: the tip of the target branch and the tip of the source branch.

There are two types of merges:

  • Fast-forward merge: If the target branch has not moved since the source branched off, Git just moves the pointer forward. No merge commit is created.
  • Three-way merge: If both branches have new commits, Git creates a merge commit that combines both sets of changes.
BASH
# Switch back to main
git checkout main

# Merge the feature branch into main
git merge feature/fund-transfer

# View the log showing the merge
git log --oneline --graph
TEXT
*   e5f2a10 Merge branch 'feature/fund-transfer'
|\  
| * c4d1b09 feat: add TransferService with basic transfer method
|/  
* b3f7a21 feat: add getBalance method for SAV2001
* a1c9e04 feat: add empty AccountService class

How is merge different from rebase?

AspectMergeRebase
HistoryPreserves branch structure (non-linear)Rewrites to linear history
Merge commitCreates a merge commit with two parentsNo merge commit
SafetyDoes not rewrite existing commitsRewrites commit hashes
Team useSafe for shared branchesOnly on local/private branches
Conflict resolutionResolve once in the merge commitResolve for each replayed commit
Debugginggit bisect works with merge commitsCleaner for git log
BASH
# Merge approach: preserves the branch structure
git checkout main
git merge feature/loan-processing
# Creates: main -> merge-commit -> both histories

# Rebase approach: linear history
git checkout feature/loan-processing
git rebase main
git checkout main
git merge feature/loan-processing  # fast-forward
# Creates: main -> feature commits in a straight line
JAVA
package com.bank.git;

public class MergeVsRebaseDemo {
    public static void main(String[] args) {
        System.out.println("=== Merge ===");
        System.out.println("main:    A - B - C - M (merge commit)");
        System.out.println("feature:       \\ D - E /");
        System.out.println("M has two parents: C and E");
        System.out.println();

        System.out.println("=== Rebase ===");
        System.out.println("Before: main: A - B - C");
        System.out.println("        feat:      \\ D - E");
        System.out.println("After:  main: A - B - C - D' - E'");
        System.out.println("D' and E' are new commits (different SHA)");
    }
}
TEXT
=== Merge ===
main:    A - B - C - M (merge commit)
feature:       \ D - E /
M has two parents: C and E

=== Rebase ===
Before: main: A - B - C
        feat:      \ D - E
After:  main: A - B - C - D' - E'
D' and E' are new commits (different SHA)

What is a pull request?

A pull request (PR) is not a Git feature. It is a platform feature provided by GitHub, GitLab (called merge request), or Bitbucket. A PR proposes merging a source branch into a target branch and provides:

  • A diff showing all changes.
  • A discussion thread for code review comments.
  • CI/CD pipeline status (tests, linting, security scans).
  • Approval workflow requiring one or more reviewers.

In the National Bank of India team, the workflow is:

  1. Rajesh creates feature/daily-transfer-limit off main.
  2. Rajesh pushes the branch and opens a PR.
  3. Priya reviews the code, leaves comments.
  4. CI runs tests, linting, and security scans.
  5. After approval, Rajesh merges the PR.
BASH
# Push the feature branch to remote
git push -u origin feature/daily-transfer-limit

# Create a PR using GitHub CLI
gh pr create \
  --title "feat: enforce daily transfer limit for savings accounts" \
  --body "Adds a 10 lakh daily limit check in TransferService." \
  --base main \
  --head feature/daily-transfer-limit
TEXT
Creating pull request for feature/daily-transfer-limit into main
https://github.com/national-bank/api/pull/42

How do you resolve merge conflicts?

A merge conflict happens when two branches change the same line in the same file. Git cannot decide which change to keep, so it marks the conflict and asks you to resolve it manually.

BASH
# Rajesh and Priya both changed the same line in AccountService.java
git merge feature/priya-update
# CONFLICT (content): Merge conflict in AccountService.java

Git marks the conflict in the file:

TEXT
<<<<<<< HEAD
    private static final BigDecimal MIN_BALANCE = new BigDecimal("5000.00");
=======
    private static final BigDecimal MIN_BALANCE = new BigDecimal("10000.00");
>>>>>>> feature/priya-update

You resolve by choosing the correct value, removing the markers, and committing.

JAVA
package com.bank.service;

import java.math.BigDecimal;

public class ConflictResolutionDemo {

    // After discussion: Priya's value is correct for premium accounts
    private static final BigDecimal MIN_BALANCE = new BigDecimal("10000.00");

    public static void main(String[] args) {
        System.out.println("Minimum balance after conflict resolution: "
            + MIN_BALANCE);
    }
}
TEXT
Minimum balance after conflict resolution: 10000.00
BASH
# After editing the file to resolve
git add AccountService.java
git commit -m "fix: resolve min balance conflict, use premium value"

What are common mistakes with Git?

Mistake 1: Committing directly to main

Wrong: pushing directly to main without a review.

BASH
# WRONG: No branch, no PR, no review
git checkout main
echo 'quick fix' >> AccountService.java
git add . && git commit -m "fix something"
git push origin main

Correct: always work on a branch and create a PR.

BASH
# CORRECT: Branch, commit, PR, review, merge
git checkout -b fix/minimum-balance-check
# ... make changes ...
git add AccountService.java
git commit -m "fix: correct minimum balance validation for SAV accounts"
git push -u origin fix/minimum-balance-check
gh pr create --title "fix: correct minimum balance validation"

Mistake 2: Large, unfocused commits

Wrong: one commit with 15 files and a vague message.

BASH
# WRONG: giant commit with no context
git add .
git commit -m "updates"

Correct: small, focused commits with clear messages.

BASH
# CORRECT: each commit has one purpose
git add src/AccountService.java
git commit -m "feat: add daily transfer limit validation"

git add src/TransferService.java
git commit -m "feat: call daily limit check before transfer"

git add test/TransferServiceTest.java
git commit -m "test: verify daily limit rejection for SAV2001"

What branching strategies do teams use?

StrategyDescriptionBest for
Git Flowmain, develop, feature/, release/, hotfix/ branchesLarge teams, scheduled releases
GitHub Flowmain + short-lived feature branchesContinuous deployment
Trunk-BasedEveryone commits to main (or short branches <1 day)High-velocity teams with feature flags

At National Bank of India, the backend team uses GitHub Flow:

  1. main is always deployable.
  2. Features go in short-lived branches (feature/transfer-limit).
  3. Every branch gets a PR with at least one reviewer.
  4. After approval and CI passes, merge to main.
  5. Main is deployed automatically.
JAVA
package com.bank.git;

public class BranchingStrategyDemo {
    public static void main(String[] args) {
        System.out.println("GitHub Flow at National Bank of India:");
        System.out.println("1. git checkout -b feature/daily-transfer-limit");
        System.out.println("2. Write code, commit with clear messages");
        System.out.println("3. git push -u origin feature/daily-transfer-limit");
        System.out.println("4. Open PR, request review from Priya");
        System.out.println("5. CI runs: unit tests, integration tests, SonarQube");
        System.out.println("6. Priya approves, merge to main");
        System.out.println("7. Auto-deploy to staging, then production");
    }
}
TEXT
GitHub Flow at National Bank of India:
1. git checkout -b feature/daily-transfer-limit
2. Write code, commit with clear messages
3. git push -u origin feature/daily-transfer-limit
4. Open PR, request review from Priya
5. CI runs: unit tests, integration tests, SonarQube
6. Priya approves, merge to main
7. Auto-deploy to staging, then production

What should a senior engineer know?

- git bisect. Binary search through commit history to find the commit that introduced a bug. Start with a known good commit and a known bad commit. Git checks out the midpoint. You test and tell Git "good" or "bad." After log(N) steps, Git finds the offending commit.

- Signed commits. In banking, regulatory compliance may require verifiable authorship. Use GPG-signed commits (git commit -S) so reviewers can verify who made each change.

- Monorepo vs multi-repo. Large banking platforms debate whether all services live in one repository (monorepo) or separate repositories (multi-repo). Monorepos simplify dependency management. Multi-repos enforce service boundaries.

- Git hooks for compliance. Pre-commit hooks can enforce coding standards, prevent secrets from being committed (using tools like gitleaks), and run linting before code leaves the developer's machine.

- Cherry-pick for hotfixes. When a production bug needs a fix from a feature branch, use git cherry-pick to apply that specific commit to the release branch without merging the entire feature.

What are common interview follow-up questions?

  • What is the difference between git fetch and git pull?
  • How do you undo a commit that has already been pushed?
  • What is git stash and when do you use it?
  • How does git bisect work to find a bug-introducing commit?
  • What is the difference between a squash merge and a regular merge?

Visual Overview

DIAGRAM
  Git Commit History
  ===================

  A ──> B ──> C ──> D    (main)
              │
              └──> E ──> F    (feature/transfer-limit)

  After merge:
  A ──> B ──> C ──> D ──> M    (main, M = merge commit)
              │          /
              └──> E ──> F

  After rebase:
  A ──> B ──> C ──> D ──> E' ──> F'    (main, linear)


  GitHub Flow
  ============

  main:     ──A──B──C──────────M──>  (always deployable)
                    │         /
  feature:          └──D──E──F       (short-lived branch)
                        │
                    PR #42 opened
                    CI: tests pass
                    Review: approved
                    Merged to main


  Merge Conflict Markers
  =======================

  <<<<<<< HEAD          (your changes)
  MIN_BALANCE = 5000
  =======               (separator)
  MIN_BALANCE = 10000
  >>>>>>> feature       (incoming changes)

  Resolution: pick one, remove markers, commit

Key Takeaway

Git is the foundation of modern software collaboration. Every change to the National Bank of India codebase is tracked as a commit: a permanent snapshot with a hash, an author, and a message. Commits are immutable and form a chain, giving the team a complete history of every change ever made.

Branches let developers work in parallel without stepping on each other. Rajesh creates a branch for the daily transfer limit feature. Priya creates a branch for the loan processing update. Both work independently. When their work is done, they merge back into main through pull requests, where the team reviews the code, CI runs tests, and only approved changes reach production.

Merging and rebasing are two ways to combine branches. Merging preserves the branch structure and creates a merge commit. Rebasing rewrites history into a straight line. The rule: never rebase commits that have been pushed to a shared branch, because rewriting shared history confuses every other developer who pulled those commits.

The most important habit is small, focused commits with clear messages. A commit message like "updates" tells no one anything. A message like "feat: add daily transfer limit validation for savings accounts" tells the reviewer, the future debugger, and the auditor exactly what changed and why.

One sentence to remember: branch for every change, commit with a clear message, merge through a reviewed pull request, and never push directly to main.

---

Share
Use ← → arrow keys to navigate