git merge <branchname>

Merge the specified branch into the current branch, combining their histories.

This command is used to bring changes from another branch into your current branch, integrating commits so you can move forward with a unified history.

This command takes the branch named in <branchname> (for example feature/login-page) and merges its commits into whichever branch you currently have checked out (often main or develop); by default it performs a normal merge, which may create a merge commit if the histories have diverged. The parameter <branchname> is the only required argument and must be the name of an existing local (or tracked remote) branch, and this command must be run while you are already on the branch that should receive the changes. If there are no conflicting edits, Git automatically applies the changes; if the same lines were changed in both branches, Git will stop and ask you to resolve the conflicts before completing the merge.

You can customize how this command merges history using flags like --no-ff (or --no-fast-forward) to always create a merge commit even when a fast-forward would be possible, --ff-only to refuse anything but a fast-forward merge, or --squash to combine all incoming commits into a single, staged commit without recording a full merge. For example, git merge --no-ff <branchname> keeps a visible merge commit for feature branches, while git merge --squash <branchname> lets you review and commit the combined changes as one commit. Related commands include git pull, which internally does a git fetch followed by a merge into your current branch, and git rebase <branchname>, which is an alternative to merging that replays your commits on top of another branch to keep history linear.

Examples:

  • git merge feature/login-page
  • git merge bugfix/fix-typo
  • git merge release/1.2.0
Manual page
git merge
Related commands

Welcome to GitExamples!

Sign in to enable bookmarking, reminders, progress-tracking and more...