mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
261 lines
4.1 KiB
Plaintext
261 lines
4.1 KiB
Plaintext
---
|
|
title: git merge
|
|
description: Join two or more development histories together
|
|
---
|
|
|
|
Combine changes from different branches.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git merge [options] <branch>
|
|
```
|
|
|
|
## Description
|
|
|
|
`git merge` integrates changes from one branch into another. It creates a new commit that combines the histories of both branches.
|
|
|
|
## Common usage
|
|
|
|
Merge a feature branch into current branch:
|
|
|
|
```bash
|
|
git merge feature-branch
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="--no-ff" type="flag">
|
|
Create a merge commit even if fast-forward is possible
|
|
|
|
```bash
|
|
git merge --no-ff feature-branch
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--ff-only" type="flag">
|
|
Only merge if fast-forward is possible
|
|
|
|
```bash
|
|
git merge --ff-only feature-branch
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--squash" type="flag">
|
|
Combine all commits into one before merging
|
|
|
|
```bash
|
|
git merge --squash feature-branch
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--abort" type="flag">
|
|
Abort current merge and restore pre-merge state
|
|
|
|
```bash
|
|
git merge --abort
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-m" type="string">
|
|
Specify custom merge commit message
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### Basic merge
|
|
|
|
```bash
|
|
git checkout main
|
|
git merge feature-branch
|
|
```
|
|
|
|
### Merge with no fast-forward
|
|
|
|
Preserve branch history:
|
|
|
|
```bash
|
|
git merge --no-ff feature-branch
|
|
```
|
|
|
|
### Squash merge
|
|
|
|
Combine all feature commits into one:
|
|
|
|
```bash
|
|
git merge --squash feature-branch
|
|
git commit -m "Add complete feature"
|
|
```
|
|
|
|
### Merge specific commit
|
|
|
|
```bash
|
|
git merge abc123
|
|
```
|
|
|
|
## Merge strategies
|
|
|
|
### Fast-forward merge
|
|
|
|
When the target branch hasn't diverged:
|
|
|
|
```bash
|
|
git merge feature-branch
|
|
# Results in fast-forward
|
|
```
|
|
|
|
The branch pointer simply moves forward. No merge commit is created.
|
|
|
|
### Three-way merge
|
|
|
|
When both branches have new commits:
|
|
|
|
```bash
|
|
git merge feature-branch
|
|
# Creates merge commit
|
|
```
|
|
|
|
Git creates a new commit with two parents, combining both histories.
|
|
|
|
## Handling merge conflicts
|
|
|
|
When conflicts occur:
|
|
|
|
```bash
|
|
git merge feature-branch
|
|
# CONFLICT (content): Merge conflict in file.txt
|
|
```
|
|
|
|
### Resolve conflicts
|
|
|
|
1. Open conflicted files and look for conflict markers:
|
|
|
|
```text
|
|
<<<<<<< HEAD
|
|
Your changes
|
|
=======
|
|
Their changes
|
|
>>>>>>> feature-branch
|
|
```
|
|
|
|
2. Edit to resolve conflicts
|
|
3. Stage resolved files:
|
|
|
|
```bash
|
|
git add resolved-file.txt
|
|
```
|
|
|
|
4. Complete the merge:
|
|
|
|
```bash
|
|
git commit
|
|
```
|
|
|
|
### Abort merge
|
|
|
|
If you want to cancel:
|
|
|
|
```bash
|
|
git merge --abort
|
|
```
|
|
|
|
## Common workflows
|
|
|
|
### Feature branch merge
|
|
|
|
```bash
|
|
# Update main
|
|
git checkout main
|
|
git pull
|
|
|
|
# Merge feature
|
|
git merge feature-branch
|
|
|
|
# Push
|
|
git push
|
|
```
|
|
|
|
### Merge main into feature branch
|
|
|
|
Keep feature branch up to date:
|
|
|
|
```bash
|
|
git checkout feature-branch
|
|
git merge main
|
|
```
|
|
|
|
### Safe merge with check
|
|
|
|
```bash
|
|
# Check what would be merged
|
|
git diff main..feature-branch
|
|
|
|
# Merge
|
|
git merge feature-branch
|
|
```
|
|
|
|
## Merge vs Rebase
|
|
|
|
### Use merge when:
|
|
- You want to preserve complete history
|
|
- Working on shared/public branches
|
|
- Branch history is important
|
|
|
|
### Use rebase when:
|
|
- You want a linear history
|
|
- Working on personal feature branches
|
|
- Commits are not yet pushed
|
|
|
|
## Best practices
|
|
|
|
- Always pull latest changes before merging
|
|
- Review diffs before merging
|
|
- Use `--no-ff` for feature branches to preserve history
|
|
- Write descriptive merge commit messages
|
|
- Test after merging before pushing
|
|
- Delete feature branch after successful merge
|
|
|
|
## Undo a merge
|
|
|
|
### Before pushing
|
|
|
|
```bash
|
|
# Undo merge, keep changes
|
|
git reset --soft HEAD~1
|
|
|
|
# Undo merge, discard changes
|
|
git reset --hard HEAD~1
|
|
```
|
|
|
|
### After pushing
|
|
|
|
```bash
|
|
# Create revert commit
|
|
git revert -m 1 HEAD
|
|
```
|
|
|
|
## Advanced merging
|
|
|
|
### Merge with custom strategy
|
|
|
|
```bash
|
|
git merge -X theirs feature-branch # Favor their changes
|
|
git merge -X ours feature-branch # Favor our changes
|
|
```
|
|
|
|
### Merge without committing
|
|
|
|
```bash
|
|
git merge --no-commit feature-branch
|
|
# Review changes
|
|
git commit
|
|
```
|
|
|
|
## Related commands
|
|
|
|
- [git rebase](/git/rebase) - Alternative to merge
|
|
- [git checkout](/git/checkout) - Switch branches
|
|
- [git branch](/git/branch) - Manage branches
|
|
- [git pull](/git/pull) - Fetch and merge
|
|
- [git cherry-pick](/git/cherry-pick) - Apply specific commits
|