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.
Initial Example
To merge another branch into your current branch, use:
$ git merge <branch_name>
MERGE Options
Option | Description |
---|---|
--commit | Perform the merge and commit the result. Overrides --no-commit . |
--no-commit | Perform the merge and stop before creating a merge commit, allowing inspection and tweaks. |
--edit , -e | Invoke an editor before committing the merge to edit the auto-generated merge message. |
--no-edit | Accept 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. |
--ff | Resolve the merge as a fast-forward if possible. |
--no-ff | Create a merge commit even if the merge could be resolved as a fast-forward. |
--ff-only | Only 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-sign | Do not GPG-sign the resulting merge commit. |
--log[=<n>] | Populate the log message with one-line descriptions from the merged commits. |
--no-log | Do not list one-line descriptions from the merged commits. |
--signoff | Add a Signed-off-by trailer by the committer at the end of the commit log message. |
--no-signoff | Do not add a Signed-off-by trailer. |
--stat , -n | Show a diffstat at the end of the merge. |
--no-stat | Do not show a diffstat at the end of the merge. |
--squash | Create a single commit on top of the current branch, as if a real merge happened, without actually committing. |
--no-squash | Perform the merge and commit the result. Overrides --squash . |
--[no-]verify | Bypass 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-signatures | Verify that the tip commit of the side branch being merged is signed with a valid key. |
--no-verify-signatures | Do not verify the signatures of the merged commits. |
--summary , --no-summary | Show or hide a diffstat at the end of the merge. These are deprecated options and will be removed in the future. |
-q , --quiet | Operate quietly, implies --no-progress . |
-v , --verbose | Be verbose. |
--progress , --no-progress | Turn progress reporting on or off explicitly. |
--autostash | Automatically create a temporary stash entry before the merge and apply it afterwards. |
--no-autostash | Do not create a temporary stash entry. |
--allow-unrelated-histories | Allow 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"