The Linux `merge` command, found in version control tools like Git, is used to combine changes from different branches (or files) into a single version—critical for team workflows or local testing. Before using it, ensure you’re in a Git repository and know your branch structure. The command requires basic Git familiarity (e.g., `git checkout`, `git add`), but this guide covers every step clearly.

What You'll Need

  • Git installed on your system (check with `git --version`).
  • A terminal emulator (preferably one with basic Git integration).
  • A Git repository with branches you want to merge (e.g., `main` and a feature branch).
  • Basic Git commands already practiced (e.g., `git status`, `git add`).

Step-by-Step

  1. Navigate to your repository: Open a terminal and cd into your project directory, then verify it’s a Git repo: cd /path/to/your/repo && git status (If no output, your repo isn’t initialized.)
  2. Switch to your target branch: Decide whether to merge into an existing branch (e.g., `main`) or create a new one. For this guide, assume you’re merging into `main` from a feature branch called `feature/login`. Run: git checkout main (If you need to create the branch first, use git branch feature/login && git checkout feature/login.)
  3. Pull the latest changes: Ensure your local `main` branch is up-to-date: git pull origin main (This avoids conflicts with changes pushed by others. Skip if you’re working alone.)
  4. Merge the feature branch: Merge the `feature/login` branch into `main`: git merge feature/login Git will pause and ask to resolve conflicts if files differ. Handle them as follows:
    1. Open the conflicting file (e.g., `src/auth.js`). Git marks conflicts with <<<<<<<, =======, and >>>>>>>.
    2. Edit the file manually to combine changes from both branches. Remove conflict markers (`<<<<<<<`, `>>>>>>`) after editing.
    3. Save the file. Git will ask for confirmation: git add src/auth.js (Use git status to check progress.)
  5. Verify the merge: After resolving conflicts (or if there were none), confirm the merge: git status (You should see: “On branch main” and “Your branch is up to date.”) Check the log for the merge commit: git log --oneline (Look for a commit with “Merge branch ‘feature/login’”.)
  6. Push the changes (optional): If working with a remote repository, push the merged branch: git push origin main (Skip if you’re testing locally.)

Common Mistakes

  • **Unresolved conflicts**: Always save files *after* editing conflict markers. If you ignore conflicts, Git will reject the merge, forcing you to redo steps 3–5. Fix: Use `git mergetool` (for VS Code/GitKraken) or manually resolve conflicts in the editor.
  • **Merging a dirty working directory**: Save changes before running `git merge`. A staged file (`git add`) will cause Git to abort the merge. Fix: Unstage files with `git restore --staged ` or commit them first.
  • **Wrong branch specified**: Merge only into branches you own or have write access to. Fix: Double-check with `git branch` and `git remote -v`.

Tips

  • **Merge interactively**: Use `git merge -Xours` or `git merge -theirs` to prefer changes from one branch over the other (set defaults with `git config merge.ff only`). Example: To keep local changes: git merge -Xours feature/login
  • **Preserve history**: Use `--no-ff` to ensure the merge creates a separate commit (useful for complex branches): git merge --no-ff feature/login
  • **Test locally first**: Create a temporary branch to test merges before applying to `main`. Example: git checkout -b test-merge && git merge feature/login && git checkout main

Troubleshooting

  1. **Merge failed with “Couldn’t open file”**: Check: Verify the file exists at the expected path: ls path/to/file Fix: Navigate to the correct directory or use absolute paths (e.g., `/home/user/repo/src/auth.js`).
  2. **Git stuck on “automatic merge”**: Check: Ensure no files are staged or untracked in your working directory. Fix: Run `git stash` to temporarily save changes, merge, then restore: git stash && git merge feature/login && git stash pop
  3. **Merge conflicts appear but no file changes**: Check: Run `git diff main feature/login --name-only` to list changed files. Fix: If files are identical but Git still reports conflicts, force a merge: git merge --allow-unrelated-histories feature/login (Use with caution—this rewrites history.)
  4. **Local changes lost after merge**: Check: If you committed local changes before merging, restore them: git checkout --ours # Keeps local changes git checkout # Restores working directory state

Frequently Asked Questions

Why does Git ask for confirmation during a merge?

Git shows conflict markers (<<<<<<<, =======, >>>>>>>) when files differ in both branches. It needs your edit to resolve the conflict before finalizing the merge.

What if I don’t know the exact branch name?

List available branches with git branch -a. The local branches appear alongside remote-tracking branches (e.g., `remotes/origin/main`).

Can I merge two branches that have never been merged before?

Yes, but use `--allow-unrelated-histories` (as shown above) to combine them without rewriting history. This is unsafe for shared repositories.

How do I undo a merge if it went wrong?

Use git reset --soft HEAD~1 to revert to the state before the merge, then discard the merge commit with git reset --hard HEAD~2 (for two commits back).

What happens if I merge into a branch that doesn’t exist?

Git will fail with an error like “fatal: refusing to merge unrelated histories.” Always ensure the branch exists with git checkout branch-name or git branch branch-name.

Is there a way to preview merge changes before resolving conflicts?

Use git merge --no-commit to create a merge commit without applying changes. Edit the merge commit in your IDE or editor, then finalize it with git commit.