Software Development and Design · Topic 7 of 7
Git Branches, Merging and Conflicts
Learn how Git branches isolate work, how merging combines them, why merge conflicts happen and how to resolve one step by step with the right commands.
- 4 min
- Includes a diagram
- A branch is an isolated line of development, created with
git branchorgit checkout -b. git checkoutandgit switchmove between branches.git merge NAMEmerges that branch into the current one.
What it is
A Git branch is a separate line of development that lets you change code without touching the main line.
When the work is finished and tested, you merge the branch back. Sometimes Git cannot combine two changes by itself, and you resolve a merge conflict by hand.
Branching is cheap in Git, so teams create one for each feature, fix or experiment.
How it works
Branches are pointers to commits, and merging joins two histories together.
Working with branches
$ git branch
$ git branch add-vlan-check
$ git checkout add-vlan-check
$ git switch main
$ git checkout -b fix-typo
git branchlists the branches and marks the current one.git branch NAMEcreates a branch.git checkout NAMEandgit switch NAMEmove to a branch.git checkout -b NAMEcreates a branch and switches to it in one step.
New commits go onto the branch you are on. The files in your folder change to match the branch you switch to.
Merging
To merge, switch to the branch that should receive the changes, then run git merge with the name of the other branch.
$ git switch main
$ git merge add-vlan-check
There are two common results.
| Merge type | When it happens |
|---|---|
| Fast forward | The main branch has no new commits, so Git just moves the pointer forward |
| Merge commit | Both branches have new commits, so Git creates a new commit that joins them |
After a successful merge you can delete the finished branch with git branch -d add-vlan-check.
Merge conflicts
A conflict happens when two branches change the same lines of the same file, or when one branch deletes a file that the other changed. Git cannot decide which version is right, so it stops and asks you.
Git marks the problem area in the file with three kinds of line:
- A line of seven less than signs starts your version, labelled HEAD.
- A line of seven equals signs separates the two versions.
- A line of seven greater than signs ends the other version, labelled with its branch name.
Auto-merging config.yaml
CONFLICT (content): Merge conflict in config.yaml
Automatic merge failed; fix conflicts and then commit the result.
Resolving a conflict
- Run
git statusto see which files are in conflict. - Open each file and decide what the final text should be. Keep one side, or combine both.
- Delete the marker lines, so only the final text remains.
- Run
git addon the fixed file. - Run
git committo complete the merge.
$ git status
$ git add config.yaml
$ git commit -m "Merge add-vlan-check and resolve config conflict"
If you want to give up, git merge --abort returns to the state before the merge.
Comparing branches
Before you merge, git diff main add-vlan-check shows what differs between the two branches. It uses the same unified diff form as other Git diffs.
Sharing a branch
A new branch exists only on your machine until you push it.
$ git push origin add-vlan-check
Many teams then open a pull request or merge request. A reviewer checks the changes before they are merged into the main branch.
Good habits
- Keep branches short lived, so the gap to main stays small.
- Pull the latest main into your branch often, to find conflicts early.
- Commit small, focused changes. Small changes conflict less.
A conflict is not an error in Git. It is a request for a human decision. Fix the file, add it and commit.
What to remember for the exam
Six facts cover this topic.
- A branch is an isolated line of development, created with
git branchorgit checkout -b. git checkoutandgit switchmove between branches.git merge NAMEmerges that branch into the current one.- A conflict occurs when two branches change the same lines.
- You resolve a conflict by editing the file, using
git add, thengit commit. git merge --abortcancels a merge that has gone wrong.
Common mistakes
Four mistakes are typical.
Merging in the wrong direction. You must be on the receiving branch when you run git merge.
Committing the conflict markers. The marker lines are not code. Remove them before you add the file.
Working on the main branch directly. Use a branch, so the main line stays stable.
Forgetting to push the branch. Until you push, colleagues cannot see it.
Do not run a commit while conflicts remain. Check git status and make sure every conflicted file is fixed and added.
Check yourself
Work through these three questions.
Which command creates a branch and switches to it in one step?
git checkout -b NAMEdoes both.When does a merge conflict occur?
When two branches change the same lines of a file, so Git cannot decide which version to keep.
What are the steps to finish a merge after a conflict?
Edit the file to the final text and remove the markers. Then run
git addon it andgit commit.
Topic 7 of 7 in Software Development and Design
Test yourself on Software Development and Design
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.