Skip to content
jsworkbench.
Learning path
Intermediate18 min

Merging

Understand the idea

A fast-forward only moves a label. A real merge creates a commit with two parents.

The idea

A joins two lines. When the other is a direct ancestor, Git only moves your label. That is a fast-forward. When both sides have new commits, the merge commit stores two parents and a combined snapshot.

What changes

Fast-forward changes a label and checks out that snapshot. A true merge adds one commit whose parents are HEAD and the branch you named. The becomes the combined snapshot. If the same lines differ, the command stops with conflict markers instead of guessing. You will practice that choice in the conflict lesson.

Two ways a merge can finish
HistoryResultGraph
The other branch is behind youFast-forwardYour label moves, no new commit
Both sides have commitsMerge commitOne node, two parents
The same lines disagreeConflictMarkers in the file, merge unfinished

The command

Make a commit on main, branch topic, commit a different file there, switch back to main, and run git merge topic. Because the edits do not overlap, the graph shows a commit with two parent hashes. If topic was simply ahead of main, the same command only slides the main label.

The mistake

Check your graph

After a diverged merge, the new commit lists both parents. main points at it. topic still points at its own tip, which is now one of those parents.

Your repository

Merge a branch that diverged. The graph shows either a moved label or a commit with two parent hashes.

  1. Step 1

    Start a line of work

    signup begins at the current commit. If it already exists, run git switch signup.

  2. Step 2

    Add a file that main does not have

    Create extra.txt in the editor and set the text to: signup form

  3. Step 3

    Stage that file

    git add copies extra.txt from the editor into the index.

  4. Step 4

    Save the signup commit

    signup now has one commit that main does not.

  5. Step 5

    Change main too

    Switch back, then in the editor add this line to README.md: Shared project notes

  6. Step 6

    Stage the main edit

    The README change is still only in the editor until you add it.

  7. Step 7

    Commit the main edit

    Both branches now have a commit the other does not.

  8. Step 8

    Join the two lines

    Merge brings signup into main. The graph shows a commit with two parents, or a moved label if one side had not diverged.

  9. Step 9

    Read the join

    The new commit lists both parents when the histories had diverged.

The editor edits the working tree. Start a repository and the files appear here.

Follow the steps. Each command step can be placed in the terminal. Edit files in the editor, then stage them.
Supported command reference
  • init
  • add <path> | add .
  • commit -m "message" | commit --amend -m "message"
  • status
  • diff | diff --staged
  • log | log --oneline | log --graph | log --oneline --graph
  • show [ref]
  • branch | branch <name> | branch -d <name>
  • checkout <ref> | checkout -b <name> | switch <name> | switch -c <name>
  • merge <branch>
  • rebase <branch> | rebase -i <branch> | rebase --edit-todo <pick|drop|reword> <hash> [message] | rebase --continue | rebase --abort
  • cherry-pick <ref>
  • reset --soft <ref> | reset --mixed <ref> | reset --hard <ref>
  • revert <ref>
  • stash | stash list | stash pop | stash apply
  • restore <path> | restore --staged <path>
  • remote | remote add <name> <repo-slug>
  • clone <repo-slug> | git clone <repo-slug> | git fork <repo-slug>
  • fetch [remote] | pull [remote] [branch] | push [remote] [branch] | push --force [remote] [branch]
  • blame <path>
  • reflog
  • tag | tag <name> [ref] | tag -d <name>
  • bisect start | bisect good [ref] | bisect bad [ref] | bisect reset
  • config <key> <value> | config --list
  • write <path> <text> (workbench: edit the working tree)
  • issue new "title" | pr new <source> <target> "title" | review "comment" | review --request-changes "comment"

Commit graph

No commits yet. git init creates the first one.

Quiz

A quick check for understanding. Retry as often as you like; your learning path stays open.

01When is a merge only a label move?
02What is special about a merge commit?

Ready to call this one understood?

Mark this lesson complete to earn 150 XP. You can always revisit it.

Sign in to save progress
Next lesson