> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Local Branch Merge

> Guide showing how to perform and verify a fast-forward local Git merge, inspect history, and clean up feature branches

Merging combines changes from one Git branch into another so you can integrate separate work into a single codebase. This guide shows a simple local merge workflow (fast-forward example), how to inspect history afterward, and how to clean up the feature branch.

Scenario

* Branch `exploring-coloring`: player starts with 5 lives and UI color changed to green.
* Branch `main`: player starts with 3 lives and original colors remain.

Below are the relevant file excerpts before merging.

script.js on main (shows 3 lives):

```javascript theme={null}
function startGame() {
    gameState.currentLevel = 1;
    gameState.score = 0;
    gameState.lives = 3;
    gameState.activePowerups = [];
    gameState.highScore = localStorage.getItem('blockBusterHighScore') || 0;

    document.getElementById('welcomeScreen').classList.remove('active');
    document.getElementById('gameOverScreen').classList.remove('active');
    document.getElementById('gameScreen').classList.add('active');

    document.getElementById('levelBadge').textContent = 'LEVEL 1';
    updateUI();
}
```

script.js on exploring-coloring (shows 5 lives and a few UI tweaks):

```javascript theme={null}
function startGame() {
    gameState.currentLevel = 1;
    gameState.score = 0;
    gameState.lives = 5;
    gameState.activePowerups = [];
    gameState.highScore = localStorage.getItem('blockBusterHighScore') || 0;

    document.getElementById('welcomeScreen').classList.add('hidden');
    document.getElementById('gameOverScreen').classList.add('hidden');
    document.getElementById('gameScreen').classList.remove('hidden');

    document.getElementById('levelBadge').textContent = 'LEVEL 1';
    updateUI();
    init();
    draw();
}

function nextLevel() {
    gameState.currentLevel++;
}
```

When to expect a fast-forward merge
A fast-forward merge happens when the target branch (e.g., `main`) has no new commits since the source branch diverged. In that case Git simply moves the `main` pointer forward so it points to the same commit as the source branch. If both branches have unique commits, Git creates a merge commit unless you rebase first.

Performing a local merge (fast-forward example)

1. Checkout the target branch you want to merge into (usually `main`).
2. Merge the source branch into it.

Example commands and output:

```bash theme={null}
# switch to main
$ git checkout main
Switched to branch 'main'

# merge the changes from exploring-coloring into main
$ git merge exploring-coloring -m "merging new color and lives"
Updating 4b0f8f3..6deef81
Fast-forward
 script.js | 2 ++
 style.css | 2 ++
 2 files changed, 4 insertions(+), 0 deletions(-)
```

After this fast-forward merge, `main` now includes the updates from `exploring-coloring` (player starts with 5 lives and the styling changes). Run the app from `main` to verify the UI and behavior.

Quick reference: common git merge commands

| Command                           | Purpose                                         |
| --------------------------------- | ----------------------------------------------- |
| `git checkout main`               | Switch to the target branch before merging      |
| `git merge <source>`              | Merge `<source>` branch into the current branch |
| `git log --all --graph --oneline` | View a compact, graph-style commit history      |
| `git branch --delete <branch>`    | Delete a local branch once merged               |

Inspecting commit history
Use a compact graph log to confirm branches were combined and to visualize commit pointers.

Example:

```bash theme={null}
$ git log --all --graph --oneline
* 6deef81 (HEAD -> main, exploring-coloring) updated color and lives
* 4b0f8f3 updated the main heading
* 5b5781a fixed a typo
* 4374c54 initial commit
```

In this example both `main` and `exploring-coloring` point to the same latest commit after the fast-forward.

Deleting the feature branch locally
If you no longer need the feature branch locally, delete it to keep your branch list tidy.

Example:

```bash theme={null}
$ git branch --delete exploring-coloring
Deleted branch exploring-coloring (was 6deef81).
```

Final history after deletion:

```bash theme={null}
$ git log --all --graph --oneline
* 6deef81 (HEAD -> main) updated color and lives
* 4b0f8f3 updated the main heading
* 5b5781a fixed a typo
* 4374c54 initial commit
```

<Callout icon="lightbulb" color="#1CB2FE">
  Fast-forward merges are simple and clean: Git advances the target branch pointer when there are no divergent commits. If your branches have diverged, Git will create a merge commit. Teams often choose between merge commits and rebasing based on their preferred history style—pick the workflow that matches your team's collaboration model.
</Callout>

Summary

* Local merging: `git checkout <target>` then `git merge <source>`.
* Fast-forward merges move the branch pointer without creating a merge commit.
* Always verify the merge by opening files, running the app, and inspecting the log.
* Clean up local feature branches with `git branch --delete <branch>` when they're no longer needed.

Related resources

* [Git merge documentation](https://git-scm.com/docs/git-merge)
* [Git branching basics](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging)
* [GitHub Pull Requests overview](https://docs.github.com/en/pull-requests)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/github-foundation-certification/module/283f1e98-efc7-4003-9946-920de806da32/lesson/9fafcba5-3094-4afc-8837-5e2f1927739d" />
</CardGroup>
