Merge and handling conflicts

Utilize common version control operations with Git

📘Cisco DevNet Associate (200-901 DEVASC)


1. What is a Git Merge?

In Git, a merge is the process of combining changes from one branch into another.

  • Think of branches as parallel lines of development in your code repository.
  • When you finish work on one branch, you often want to integrate it back into the main branch (commonly called main or master).

Command to merge:

git checkout main   # Switch to the branch you want to merge into
git merge feature-branch   # Merge changes from feature-branch into main

After this, Git tries to automatically combine the changes.


2. Types of Merges

  1. Fast-forward merge
    • Happens when the branch you are merging has all commits ahead of your current branch.
    • Git simply moves the pointer forward; no new commit is created.
    • Example: git merge feature-branch
    • Exam tip: Fast-forward merge occurs only when there’s a straight line of commits with no divergence.
  2. Three-way merge
    • Happens when both branches have new commits after they split.
    • Git uses the common ancestor of both branches to combine changes.
    • If there are conflicting changes, you may need to resolve conflicts manually.
    • Exam tip: This is the more common type of merge in team projects.

3. Merge Conflicts

A merge conflict happens when Git cannot automatically combine changes.

  • Common reasons:
    1. Two branches modified the same line of a file.
    2. One branch deleted a file that the other branch edited.

Example scenario:

  • main branch: Line 10 says API_KEY = "123"
  • feature branch: Line 10 says API_KEY = "456"
  • When merged, Git doesn’t know which version to keep, causing a conflict.

4. How to Identify Conflicts

After a merge, if there is a conflict, Git will:

  • Stop the merge process.
  • Mark the conflicting files with conflict markers:
<<<<<<< HEAD
API_KEY = "123"
=======
API_KEY = "456"
>>>>>>> feature-branch
  • HEAD shows the current branch (main), while the other part shows the branch you are merging (feature-branch).

5. How to Resolve Conflicts

Steps to resolve a conflict:

  1. Open the conflicting file in a code editor.
  2. Decide which changes to keep:
    • Keep changes from main.
    • Keep changes from feature-branch.
    • Or combine both.
  3. Remove conflict markers (<<<<<<<, =======, >>>>>>>) after editing.
  4. Mark the file as resolved:
git add <file>
  1. Complete the merge:
git commit
  • Git will auto-generate a merge commit message, or you can edit it.

6. Useful Git Commands for Merges and Conflicts

CommandPurpose
git merge <branch>Merge another branch into the current branch
git statusShows which files have conflicts
git log --graph --onelineVisualizes the branch structure and merges
git diffShows changes between branches or commits
git checkout --ours <file>Keep current branch version during conflict
git checkout --theirs <file>Keep incoming branch version during conflict
git merge --abortAbort the merge and go back to previous state

7. Tips for Avoiding Conflicts

While conflicts are normal in team environments, you can reduce them:

  1. Pull updates often: git pull origin main
  2. Work on small, focused branches (smaller changes = fewer conflicts).
  3. Communicate with team members about changes in shared files.
  4. Use code review tools (like GitHub Pull Requests) to catch conflicts before merging.

8. Summary for Exam

  • Merge = Combining changes from one branch into another.
  • Fast-forward merge = Simple pointer move when no divergence.
  • Three-way merge = Combines changes from branches with different commits.
  • Merge conflict = Happens when Git cannot auto-merge changes.
  • Resolution steps: Edit conflicting files → remove markers → git addgit commit.
  • Key commands: git merge, git status, git diff, git checkout --ours/--theirs, git merge --abort.

💡 Exam Tip: You might get questions like:

  • What happens if two branches modify the same line of code? → Merge conflict.
  • Which Git command resolves a conflict?git add <file> after editing, then git commit.
  • When does a fast-forward merge occur? → When the current branch has no new commits since branching.
Buy Me a Coffee