MODULE 6 · TOPIC 8 · VISUAL GIT LAB

Creating Branches and Resolving Merge Conflicts

Git performs a three-way merge using a common ancestor, the current branch tip and the incoming branch tip. Non-overlapping edits are often merged automatically. Overlapping edits can require a human decision; conflict markers identify alternative text, while the index stores unmerged stages. Review the intended behavior, resolve files, stage them, test and commit. You can abort a merge to return to its previous state, especially when starting from a clean tree.

Interactive replays of recorded Git 2.47 command traces. The browser changes only the simulation; no local files, Git repositories or hosting accounts are modified. Commit graphs run from parent to child.

1. Automatic merge versus conflict

A merge may automatically combine independent changes or stop when overlapping edits cannot be reconciled. The common ancestor lets Git distinguish independent changes from competing replacements.

Why this example? Compare separate-file changes with same-line edits.

Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
Git workspaceInteractive replay
$
Source control
Snapshot inspector

Why this step happens
See all steps in this scenario
    Git Bash — commands and setup
    Git Bash — terminal outputRecorded output
    Repository stateCompact blocks
    CurrentResultError / rejected

    Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

    What this animation explains

    A merge may automatically combine independent changes or stop when overlapping edits cannot be reconciled. The common ancestor lets Git distinguish independent changes from competing replacements.

    Try and observe

    Inspect working tree and commit graph after merge.

    2. Conflict markers and stages

    During an ordinary merge, stage 1 is the base, stage 2 is ours and stage 3 is theirs. The working file contains conflict markers in text conflicts. Neither side should be accepted blindly.

    Why this example? Inspect conflict markers or the separate index versions.

    Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
    Git workspaceInteractive replay
    $
    Source control
    Snapshot inspector

    Why this step happens
    See all steps in this scenario
      Git Bash — commands and setup
      Git Bash — terminal outputRecorded output
      Repository stateCompact blocks
      CurrentResultError / rejected

      Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

      What this animation explains

      During an ordinary merge, stage 1 is the base, stage 2 is ours and stage 3 is theirs. The working file contains conflict markers in text conflicts. Neither side should be accepted blindly.

      Try and observe

      Read the base, ours and theirs content.

      3. Choose a resolution

      Resolution means choosing correct resulting content. Taking ours, theirs or a combined edit changes the working file; git add then marks the path resolved.

      Why this example? Select alternative resolution and finish the merge.

      Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
      Git workspaceInteractive replay
      $
      Source control
      Snapshot inspector

      Why this step happens
      See all steps in this scenario
        Git Bash — commands and setup
        Git Bash — terminal outputRecorded output
        Repository stateCompact blocks
        CurrentResultError / rejected

        Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

        What this animation explains

        Resolution means choosing correct resulting content. Taking ours, theirs or a combined edit changes the working file; git add then marks the path resolved.

        Try and observe

        Compare final file content while commit parents remain the same branches.

        4. Abort versus finish merge

        When a merge is in progress, abort attempts to reconstruct pre-merge state. Finishing requires resolving all paths and committing. In a clean-start lab these two choices are easy to compare.

        Why this example? Compare the history after abort and after completion.

        Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
        Git workspaceInteractive replay
        $
        Source control
        Snapshot inspector

        Why this step happens
        See all steps in this scenario
          Git Bash — commands and setup
          Git Bash — terminal outputRecorded output
          Repository stateCompact blocks
          CurrentResultError / rejected

          Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

          What this animation explains

          When a merge is in progress, abort attempts to reconstruct pre-merge state. Finishing requires resolving all paths and committing. In a clean-start lab these two choices are easy to compare.

          Try and observe

          Check whether HEAD has advanced and whether MERGE_HEAD remains.

          5. Multiple conflict paths

          Resolving one conflicted path does not resolve other paths. A merge commit cannot be created while unmerged index entries remain.

          Why this example? Create two overlapping files and resolve one or both.

          Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
          Git workspaceInteractive replay
          $
          Source control
          Snapshot inspector

          Why this step happens
          See all steps in this scenario
            Git Bash — commands and setup
            Git Bash — terminal outputRecorded output
            Repository stateCompact blocks
            CurrentResultError / rejected

            Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

            What this animation explains

            Resolving one conflicted path does not resolve other paths. A merge commit cannot be created while unmerged index entries remain.

            Try and observe

            Observe git status and the commit refusal until all conflicts are resolved.

            6. Rebase versus merge

            Merge preserves original commit identities and may add a merge commit. Rebase replays commits onto a new base, creating new commit IDs. Rebasing private work can make a linear history, but rewriting shared branches needs coordination.

            Why this example? Compare rebase and merge for non-overlapping divergent work.

            Before you run: Select a scenario and follow the Git execution diagram. Inspect the recorded file or reference in the Git workspace; switch between HEAD, Index and Working tree when available. The command and terminal panels show the exact replay. No Git commands execute in the browser.
            Git workspaceInteractive replay
            $
            Source control
            Snapshot inspector

            Why this step happens
            See all steps in this scenario
              Git Bash — commands and setup
              Git Bash — terminal outputRecorded output
              Repository stateCompact blocks
              CurrentResultError / rejected

              Change inputs and step through the example. Outputs are recorded from Git 2.47. Hashes, paths and configuration can differ on your computer. Run worked examples only in fresh practice folders.

              What this animation explains

              Merge preserves original commit identities and may add a merge commit. Rebase replays commits onto a new base, creating new commit IDs. Rebasing private work can make a linear history, but rewriting shared branches needs coordination.

              Try and observe

              Inspect the parent relationships and changed feature commit hash.

              Git command reference

              Use these commands in a disposable practice repository. Review the output before running commands that change files or references.

              Git commandWhy and when to use it
              git merge featureIntegrate feature into current branch
              git statusInspect conflicting paths
              git ls-files -uInspect base, ours and theirs stages
              git add app.txtMark resolved file staged
              git commitFinish merge after resolving
              git merge --abortAbort in-progress merge

              Worked practical examples

              Follow these procedures with the Git 2.47 teaching repository (Git Bash, Linux or macOS). Start each example in a separate new practice folder; never run destructive file commands in an important repository.

              1. Resolve a same-line conflict

              Procedure

              1. Initialize a disposable repository and create conflicting branch edits.
              2. Merge feature and read conflict markers.
              3. Edit the file to the desired combined content.
              4. Stage, inspect, test and finish the merge.
              Code / configuration
              git init -q -b main
              git config user.name Student
              git config user.email student@example.invalid
              printf "version 1\n" > app.txt
              git add app.txt
              git commit -qm "Initial app"
              git switch -qc feature
              printf "feature\n" > app.txt
              git commit -qam "Feature edit"
              git switch -q main
              printf "main\n" > app.txt
              git commit -qam "Main edit"
              git merge feature
              # After conflict, execute these commands separately:
              printf "main and feature\n" > app.txt
              git add app.txt
              git diff --cached
              git commit -m "Resolve merge"
              Expected result: Both branch histories are retained and the resolved text is committed.

              2. Abort an unwanted merge

              Procedure

              1. Start in a clean disposable repository with diverged edits.
              2. Merge and inspect the conflict.
              3. Abort before making other edits.
              4. Check status and branch tips.
              Code / configuration
              git init -q -b main
              git config user.name Student
              git config user.email student@example.invalid
              printf "version 1\n" > app.txt
              git add app.txt
              git commit -qm "Initial app"
              git switch -qc feature
              printf "feature\n" > app.txt
              git commit -qam "Feature edit"
              git switch -q main
              printf "main\n" > app.txt
              git commit -qam "Main edit"
              git merge feature
              git merge --abort
              git status -sb
              Expected result: The original main tip and content are restored in this clean-start example.

              Predict the outcome before you run

              Try answering these questions, then expand each answer and repeat the appropriate simulation.

              1. Predict the outcome: Same line in Automatic merge versus conflict. What changes and why?

              Check if unresolved paths remain. Inspect the final state in the lab.

              2. Predict the outcome: Combine both in Choose a resolution. What changes and why?

              Check the chosen resolution. Inspect the final state in the lab.

              3. Predict the outcome: Resolve both in Multiple conflict paths. What changes and why?

              Git refuses if any path remains unmerged. Inspect the final state in the lab.

              Hands-on practice and assessment

              Git practice challenge

              Create two branches that edit the same line, merge, inspect the markers, resolve intentionally, stage and commit. Compare automatic merge, abort and rebase alternatives in disposable repositories.

              Edit this command scratchpad. Review the commands and execute only in a disposable Git practice repository; the browser does not run shell commands.

              Quick assessment

              Key points and real Git notes

              Conflict resolution is a semantic decision, not simply deleting markers. Run relevant tests before committing and avoid rewriting shared history. The examples use a clean pre-merge state.