Git merge

git merge helps you integrate changes from two branches into a single branch. This is essential for combining work done in different branches, allowing you to keep your project up to date and organized.



Tutorials dojo strip

Initial Example

To merge another branch into your current branch, use:

$ git merge <branch_name>




MERGE Options

OptionDescription
--commitPerform the merge and commit the result. Overrides --no-commit.
--no-commitPerform the merge and stop before creating a merge commit, allowing inspection and tweaks.
--edit, -eInvoke an editor before committing the merge to edit the auto-generated merge message.
--no-editAccept the auto-generated merge message without invoking an editor.
--cleanup=<mode>Determine how the merge message is cleaned up before committing. See git-commit for more details.
--ffResolve the merge as a fast-forward if possible.
--no-ffCreate a merge commit even if the merge could be resolved as a fast-forward.
--ff-onlyOnly perform the merge if it can be resolved as a fast-forward.
--gpg-sign[=<keyid>]GPG-sign the resulting merge commit. Optional keyid defaults to the committer identity.
--no-gpg-signDo not GPG-sign the resulting merge commit.
--log[=<n>]Populate the log message with one-line descriptions from the merged commits.
--no-logDo not list one-line descriptions from the merged commits.
--signoffAdd a Signed-off-by trailer by the committer at the end of the commit log message.
--no-signoffDo not add a Signed-off-by trailer.
--stat, -nShow a diffstat at the end of the merge.
--no-statDo not show a diffstat at the end of the merge.
--squashCreate a single commit on top of the current branch, as if a real merge happened, without actually committing.
--no-squashPerform the merge and commit the result. Overrides --squash.
--[no-]verifyBypass the pre-merge and commit-msg hooks when --no-verify is given.
-s <strategy>Use the specified merge strategy. Can be supplied multiple times to specify the order of strategies to try.
-X <option>Pass a merge strategy-specific option to the merge strategy.
--verify-signaturesVerify that the tip commit of the side branch being merged is signed with a valid key.
--no-verify-signaturesDo not verify the signatures of the merged commits.
--summary, --no-summaryShow or hide a diffstat at the end of the merge. These are deprecated options and will be removed in the future.
-q, --quietOperate quietly, implies --no-progress.
-v, --verboseBe verbose.
--progress, --no-progressTurn progress reporting on or off explicitly.
--autostashAutomatically create a temporary stash entry before the merge and apply it afterwards.
--no-autostashDo not create a temporary stash entry.
--allow-unrelated-historiesAllow merging histories that do not share a common ancestor.




Examples

1. Basic Merge

To merge the feature branch into your main branch:

  • First, switch to the branch you want to merge into:
$ git checkout main
  • Then, run the merge command:
$ git merge feature

2. Merge Conflict

If there are conflicting changes, Git will highlight the files with conflicts:

  • Check the conflicted files:
$ git status
  • Resolve the conflicts by editing the conflicted files:
$ nano conflicted_file.txt
  • After resolving the conflicts, add the files to the staging area:
$ git add conflicted_file.txt
  • Commit the merge:
$ git commit -m "Resolved merge conflict"
Scroll to Top