LinuxCommandLibrary

git-merge

Combine changes from one branch into another

TLDR

Merge a branch into your current branch

$ git merge [branch_name]
copy

Edit the merge message
$ git merge [[-e|--edit]] [branch_name]
copy

Merge a branch and create a merge commit
$ git merge --no-ff [branch_name]
copy

Abort a merge in case of conflicts
$ git merge --abort
copy

Merge using a specific strategy
$ git merge [[-s|--strategy]] [strategy] [[-X|--strategy-option]] [strategy_option] [branch_name]
copy

SYNOPSIS

git merge [-n] [-v] [--progress] [--ff] [--no-ff] [--ff-only] [-S[]] [-m ] [-F ] [--author=] [--date=] [--reset-author] [--allow-empty] [--allow-empty-message] [--no-commit] [-e] [--edit] [--no-edit] [--cleanup=] [--signoff] [-i | --interactive] [--strategy=] [-X ] [--summary] [--quiet] […]

PARAMETERS

-n
    Do not run git diff-files --name-only during the merge.

-v
    Be more verbose.

--progress
    Show progress during merge.

--ff
    Allow fast-forward merge if possible.

--no-ff
    Always create a merge commit, even if a fast-forward is possible.

--ff-only
    Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge can be resolved as a fast-forward.

-m
    Merge commit message.

--author=
    Override the author for the merge commit

--strategy=
    Use the given merge strategy.

-X
    Pass merge strategy-specific option through to the merge strategy.


    Commits to merge into the current branch.

DESCRIPTION

The git merge command integrates changes from one branch into another. It's a crucial operation in Git for combining different lines of development. Typically, you use git merge to bring changes from a feature branch into your main branch (e.g., 'master' or 'main'). Git tries to automatically resolve differences between the branches. If it encounters conflicts, you'll need to manually resolve them. The merge commit records the integration event in the project history.

Git performs a three-way merge, using the common ancestor of the two branches as a reference point. This allows Git to intelligently combine changes. After a successful merge, the branch you're merging into will contain all the changes from the merged branch, and a new commit will be created on the merged-into branch to reflect the combination of changes. Understanding merge conflicts and how to resolve them is essential for effective Git workflow. Merge commits preserve the history of development, making it easier to trace the evolution of the project.

CAVEATS

Conflicts can arise during a merge if changes in the branches being merged overlap. Resolving these conflicts manually is often required.

MERGE STRATEGIES

Git offers various merge strategies (e.g., 'recursive', 'octopus', 'ours', 'subtree') to handle different merging scenarios. The 'recursive' strategy is the default and handles most common cases effectively.

MERGE CONFLICTS

When a merge results in conflicts, Git will mark the conflicting sections in the affected files. You need to edit these files, resolve the conflicts by choosing which changes to keep, and then commit the resolved files.

SEE ALSO

git checkout(1), git branch(1), git diff(1), git rebase(1)

Copied to clipboard