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
mainormaster).
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
- 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.
- 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:
- Two branches modified the same line of a file.
- One branch deleted a file that the other branch edited.
Example scenario:
mainbranch: Line 10 saysAPI_KEY = "123"featurebranch: Line 10 saysAPI_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
HEADshows 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:
- Open the conflicting file in a code editor.
- Decide which changes to keep:
- Keep changes from
main. - Keep changes from
feature-branch. - Or combine both.
- Keep changes from
- Remove conflict markers (
<<<<<<<,=======,>>>>>>>) after editing. - Mark the file as resolved:
git add <file>
- 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
| Command | Purpose |
|---|---|
git merge <branch> | Merge another branch into the current branch |
git status | Shows which files have conflicts |
git log --graph --oneline | Visualizes the branch structure and merges |
git diff | Shows 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 --abort | Abort the merge and go back to previous state |
7. Tips for Avoiding Conflicts
While conflicts are normal in team environments, you can reduce them:
- Pull updates often:
git pull origin main - Work on small, focused branches (smaller changes = fewer conflicts).
- Communicate with team members about changes in shared files.
- 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 add→git 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, thengit commit. - When does a fast-forward merge occur? → When the current branch has no new commits since branching.
