linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Branches & merging/merge

kb/branching ── Branches & merging ── intermediate

git merge

Merges another branch into the current one. Either fast-forwards the pointer or creates a merge commit with two parents. Overlapping changes produce conflicts.

view as markdownaka: git-merge

git merge takes commits from another branch and attaches them to the current one. It is the fundamental operation for collaborative work.

Two scenarios

Fast-forward

If the current branch has no new commits that are absent from the branch being merged, Git simply advances the current branch's pointer to the tip of the other branch. No new commit is created; history stays linear.

before:  main:  A → B
         feat:  A → B → C → D
after:   main:  A → B → C → D    (main caught up with feat)
         feat:  A → B → C → D

This happens when you run git merge feat from main and main has received no new commits since the branch point.

Three-way merge

When both branches have their own commits since the fork, Git creates a merge commit, a special commit with two parents.

before:  main:  A → B → E
         feat:  A → B → C → D
after:   main:  A → B → E → M    (M is the merge commit)
                   \       /
                    C → D
         feat:  A → B → C → D

The content of the merge commit is the combination of the trees at E and D against their common ancestor B. If changed lines do not overlap, Git handles it automatically. If they do, there is a conflict.

Commands

bash
git switch main
git merge feature           # merge feature into main
git merge --no-ff feature   # create a merge commit even if fast-forward is possible
git merge --ff-only feature # refuse if fast-forward is not possible
git merge --squash feature  # collapse all feature commits into one staged change
git merge --abort           # cancel a merge in progress

The --no-ff flag is a common team strategy for preserving topology: "there was a feature branch here." Without --no-ff, feature commits blend into the main line and the fact of branching disappears visually.

Conflicts

When a conflict occurs, Git marks the file with special markers:

<<<<<<< HEAD
print("from main")
=======
print("from feature")
>>>>>>> feature

What to do:

  1. Open the file in an editor or mergetool.
  2. Remove the markers and leave the version you want (or a combination).
  3. git add <file> to mark it resolved.
  4. git commit to finish the merge (Git suggests a ready-made message).

Useful commands during a conflict:

bash
git status                  # shows which files are in conflict
git diff                    # view three versions in the conflict hunks
git checkout --ours file    # accept the current branch's version entirely
git checkout --theirs file  # accept the incoming branch's version entirely

Pitfalls

  • --squash creates a commit without recording the source branch as a parent. This means the merged branch is still considered unmerged and cannot be deleted with branch -d. You must use branch -D. This often surprises people.
  • If you change your mind during a merge, git merge --abort restores everything to the state before the merge started.
  • git merge with unstaged changes in the working tree may refuse to proceed if those changes overlap with the merge. Commit or stash first.

§ команды

bash
git merge feature

Merge branch feature into the current branch

bash
git merge --no-ff feature

Create a merge commit even when fast-forward is possible

bash
git merge --abort

Cancel a merge that is in progress after a conflict

bash
git merge --squash feature

Collapse all feature commits into one staged change in the current branch

§ см. также

  • branchBranchA branch in Git is a file inside `.git/refs/heads/` that holds the SHA of one commit. Creating, switching, and deleting branches are trivial operations because a branch contains exactly 41 bytes of data.
  • commit-cmdgit commitCaptures what is in the index as a new commit object and moves the current branch to that commit. Without arguments it opens an editor for the message; the most common form uses `-m`.
  • loggit logTraverses the commit graph and prints each commit. By default it starts at HEAD and follows parents. A dozen flags cover 95% of history-browsing scenarios.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies