Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 3 of 6 · Lab · 25 min · revision 2

Branching, merging and resolving a real conflict

In a throwaway repository, merge short-lived branches by fast-forward and by merge commit, resolve a planted conflict without losing either change, and rebase a local branch.

Contentsand lesson path

What you will learn

  • Create, merge and rebase short-lived branches and resolve a merge conflict without losing anyone's changes.

01. Goal and setup

Every team workflow is built on one loop: branch, commit, merge. In this lab you run that loop several times in a throwaway repository. By the end you will have:

  • merged a branch by fast-forward and by a merge commit, and seen the difference in the commit graph
  • planted a conflict, read the conflict markers and resolved it so that neither change is lost
  • rebased a local branch onto main and seen why rebasing produces new commits

You need Git 2.40 or later and a terminal with a POSIX shell such as bash or zsh (on Windows, use Git Bash). Work in a new folder that is not inside another repository; you can delete it at the end. The files are small YAML settings for an imaginary search service, so no programming language is involved.

Commit hashes, paths and dates in the illustrative output will differ on your machine, and two branch tips made in the same second may be listed in the other order. Compare the shape of the output, not the IDs.

02. Step 1: Create the lab repository

Commands
git --version
mkdir branching-lab && cd branching-lab
git init -b main
git config user.name "Lab Learner"
git config user.email [email protected]
cat > service.yaml <<'EOF'
name: search-api
timeout_seconds: 5
retries: 2
log_level: info
allowed_origins:
  - https://app.example.com
EOF
git add service.yaml
git commit -m "Add search API settings"
Illustrative output (your Git version, paths and commit hashes differ)
git version 2.45.4
Initialized empty Git repository in .../branching-lab/.git/
[main (root-commit) 19b5f8c] Add search API settings
 1 file changed, 6 insertions(+)
 create mode 100644 service.yaml

git init -b main names the first branch main whatever your global default is. Without --global, the two git config commands set your name and email for this repository only, so the lab does not touch your usual settings. The cat command with <<'EOF' writes the lines up to EOF into the file; you could type the same content in an editor instead.

03. Step 2: Branch, commit and fast-forward

Create a short-lived branch for one small change. git switch -c creates the branch and switches to it in one go. Then go back to main and merge.

Commands
git switch -c raise-timeout
cat > service.yaml <<'EOF'
name: search-api
timeout_seconds: 10
retries: 2
log_level: info
allowed_origins:
  - https://app.example.com
EOF
git commit -am "Raise the search timeout to 10 seconds"
git switch main
git merge raise-timeout
git log --oneline --graph --all
git branch -d raise-timeout
Illustrative output (your commit hashes differ)
Switched to a new branch 'raise-timeout'
[raise-timeout ed47a98] Raise the search timeout to 10 seconds
 1 file changed, 1 insertion(+), 1 deletion(-)
Switched to branch 'main'
Updating 19b5f8c..ed47a98
Fast-forward
 service.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
* ed47a98 (HEAD -> main, raise-timeout) Raise the search timeout to 10 seconds
* 19b5f8c Add search API settings
Deleted branch raise-timeout (was ed47a98).

Nothing happened on main while you worked, so main was simply behind the branch. There was nothing to combine: Git moved the main pointer forward to the branch commit. That is a fast-forward, and it creates no new commit. git branch -d then deletes only the branch name; the commit stays in the history of main. The lower-case -d refuses to delete a branch whose commits have not been merged, which is a useful safety net.

04. Step 3: Diverge and create a merge commit

This time let both lines of work move. Change the log level on a branch, then return to main and add a README there, as a teammate might.

Commands
git switch -c quiet-logs
cat > service.yaml <<'EOF'
name: search-api
timeout_seconds: 10
retries: 2
log_level: warning
allowed_origins:
  - https://app.example.com
EOF
git commit -am "Log only warnings and errors"
git switch main
echo '# search-api' > README.md
git add README.md
git commit -m "Add a README"
git log --oneline --graph --all
Illustrative output (your commit hashes differ)
Switched to a new branch 'quiet-logs'
[quiet-logs 19180b8] Log only warnings and errors
 1 file changed, 1 insertion(+), 1 deletion(-)
Switched to branch 'main'
[main dff2704] Add a README
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
* dff2704 (HEAD -> main) Add a README
| * 19180b8 (quiet-logs) Log only warnings and errors
|/
* ed47a98 Raise the search timeout to 10 seconds
* 19b5f8c Add search API settings

The history has forked, so a fast-forward is impossible: neither tip contains the other. Merge the branch into main:

Commands
git merge --no-edit quiet-logs
git log --oneline --graph --all
git branch -d quiet-logs
Illustrative output (your commit hashes differ)
Merge made by the 'ort' strategy.
 service.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
*   eff9730 (HEAD -> main) Merge branch 'quiet-logs'
|\
| * 19180b8 (quiet-logs) Log only warnings and errors
* | dff2704 Add a README
|/
* ed47a98 Raise the search timeout to 10 seconds
* 19b5f8c Add search API settings
Deleted branch quiet-logs (was 19180b8).

Git found the merge base, the most recent commit both sides share (ed47a98), compared each side with it and combined the two sets of changes. They touched different files, so this was automatic. The result is a merge commit with two parents, which records that two lines of work were joined. --no-edit accepts the default message; without it, Git opens your editor so you can add context. ort is the name of the default merge strategy.

05. Step 4: Plant a conflict

A conflict happens when both sides change the same lines, or lines right next to each other, since the merge base. Git cannot know which version is right, so it stops and asks you. Plant one: on a branch, allow an admin site to call the API; on main, allow a staging site. Both commits add a line in the same place.

Commands
git switch -c admin-origin
echo '  - https://admin.example.com' >> service.yaml
git commit -am "Allow the admin site to call the API"
git switch main
echo '  - https://staging.example.com' >> service.yaml
git commit -am "Allow the staging site to call the API"
git log --oneline --graph --all
Illustrative output (your commit hashes differ)
Switched to a new branch 'admin-origin'
[admin-origin 6d61770] Allow the admin site to call the API
 1 file changed, 1 insertion(+)
Switched to branch 'main'
[main 3ff21d5] Allow the staging site to call the API
 1 file changed, 1 insertion(+)
* 6d61770 (admin-origin) Allow the admin site to call the API
| * 3ff21d5 (HEAD -> main) Allow the staging site to call the API
|/
*   eff9730 Merge branch 'quiet-logs'
|\
| * 19180b8 Log only warnings and errors
* | dff2704 Add a README
|/
* ed47a98 Raise the search timeout to 10 seconds
* 19b5f8c Add search API settings

06. Step 5: Merge main into your branch and resolve

In a team you normally bring main into your own branch and resolve conflicts there, so that main only ever receives work that already fits. Switch to the branch, merge main into it and look at the damage:

Commands
git switch admin-origin
git merge main
git status
cat service.yaml
Expected output
Switched to branch 'admin-origin'
Auto-merging service.yaml
CONFLICT (content): Merge conflict in service.yaml
Automatic merge failed; fix conflicts and then commit the result.
On branch admin-origin
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   service.yaml

no changes added to commit (use "git add" and/or "git commit -a")
name: search-api
timeout_seconds: 10
retries: 2
log_level: warning
allowed_origins:
  - https://app.example.com
<<<<<<< HEAD
  - https://admin.example.com
=======
  - https://staging.example.com
>>>>>>> main

Read the markers carefully:

  • between <<<<<<< HEAD and ======= is the version from the branch you are on (HEAD, here admin-origin)
  • between ======= and >>>>>>> main is the version from the branch you are merging in
  • everything outside the markers merged cleanly and needs no attention

Git has paused in the middle of the merge, and nothing is lost. If you want to back out, git merge --abort returns the branch and the files to exactly where they were before git merge. Otherwise, decide what the file should say. Both origins are legitimate, so the correct result keeps both lines and removes all three marker lines. Picking one side would quietly drop a teammate's change.

Commands
cat > service.yaml <<'EOF'
name: search-api
timeout_seconds: 10
retries: 2
log_level: warning
allowed_origins:
  - https://app.example.com
  - https://staging.example.com
  - https://admin.example.com
EOF
git diff --check
git add service.yaml
git status
git commit --no-edit
git log --oneline --graph --all
Illustrative output (your commit hashes differ)
On branch admin-origin
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
	modified:   service.yaml

[admin-origin a7d0ff5] Merge branch 'main' into admin-origin
*   a7d0ff5 (HEAD -> admin-origin) Merge branch 'main' into admin-origin
|\
| * 3ff21d5 (main) Allow the staging site to call the API
* | 6d61770 Allow the admin site to call the API
|/
*   eff9730 Merge branch 'quiet-logs'
|\
| * 19180b8 Log only warnings and errors
* | dff2704 Add a README
|/
* ed47a98 Raise the search timeout to 10 seconds
* 19b5f8c Add search API settings

git diff --check printed nothing, which means no conflict markers (or whitespace errors) are left. Running git add on a conflicted file is how you tell Git it is resolved, and git commit then records the merge. In a real project, also run the tests or start the service before committing: a file can be free of markers and still be wrong.

Now land the branch on main:

Commands
git switch main
git merge admin-origin
git branch -d admin-origin
Illustrative output (your commit hashes differ)
Switched to branch 'main'
Updating 3ff21d5..a7d0ff5
Fast-forward
 service.yaml | 1 +
 1 file changed, 1 insertion(+)
Deleted branch admin-origin (was a7d0ff5).

The branch already contained everything on main, so landing it was a fast-forward with no new conflicts. On a hosted repository the same step happens through a pull request, which the next lesson covers.

07. Step 6: Rebase a local branch instead

Merging records history exactly as it happened, forks included. Rebasing makes a different trade: it takes the commits that exist only on your branch and replays them, one at a time, on top of another commit, which gives a straight line. Try it on a branch that exists only on your machine.

Commands
git switch -c health-check
echo 'health_path: /healthz' >> service.yaml
git commit -am "Expose a health check path"
git switch main
echo 'Owned by the platform team.' >> README.md
git commit -am "Name the owning team in the README"
git switch health-check
git rebase main
git log --oneline --graph --all -4
Illustrative output (your commit hashes differ)
Switched to a new branch 'health-check'
[health-check 322b93d] Expose a health check path
 1 file changed, 1 insertion(+)
Switched to branch 'main'
[main 9572412] Name the owning team in the README
 1 file changed, 1 insertion(+)
Switched to branch 'health-check'
Successfully rebased and updated refs/heads/health-check.
* 78bc966 (HEAD -> health-check) Expose a health check path
* 9572412 (main) Name the owning team in the README
*   a7d0ff5 Merge branch 'main' into admin-origin
|\
| * 3ff21d5 Allow the staging site to call the API

Compare the IDs: the commit was 322b93d before the rebase and 78bc966 after it. Same change, same message, but a new parent, and because a commit ID covers its parent, the result is a new commit. The old one is no longer on any branch. The history is now a straight line, so main can fast-forward:

Commands
git switch main
git merge health-check
git branch -d health-check
Illustrative output (your commit hashes differ)
Switched to branch 'main'
Updating 9572412..78bc966
Fast-forward
 service.yaml | 1 +
 1 file changed, 1 insertion(+)
Deleted branch health-check (was 78bc966).

When to use which:

  • Merge when you want a faithful record, and always when you integrate a branch that other people also use. A merge never changes existing commits.
  • Rebase to tidy or update your own branch before anyone builds on it, for example to bring it up to date with main before you open a pull request.
  • Never rebase commits that others already have. Their clones still hold the old commits, so the next pull mixes old and new copies of the same work. If a branch is shared, merge instead.

If a rebase stops on a conflict, resolve the file as in step 5, run git add, then git rebase --continue; git rebase --abort takes you back to where you started.

08. Troubleshooting

09. Check your understanding

Practice check · 3 questions

Branches and merges

Think back to what you saw in the lab.

Q1. You create `fix-typo` from `main`, make one commit, and nobody commits to `main` in the meantime. What does `git merge fix-typo` do on `main`?
Q2. You are on branch `feature` and run `git merge main`. A conflicted file shows `<<<<<<< HEAD`, some lines, then `=======`. Whose lines sit between those two markers?
Q3. Halfway through resolving a conflict, you realise you merged the wrong branch. Which command takes you back to the state before the merge?
Practice only: not graded, not saved, not evidence.

10. Apply it (stretch)

  1. Rerun steps 4 and 5 with new origins, but resolve with a rebase instead of a merge: on the branch, run git rebase main, fix the file, git add it and run git rebase --continue. Compare the final graph with the one from step 5.
  2. Run git config merge.conflictStyle zdiff3 in the lab repository and plant another conflict. The markers gain a third section, introduced by |||||||, that shows the merge base, which makes it much easier to see what each side actually changed.
  3. In a real project, run git log --oneline --graph -30 on main. Find one fast-forward and one merge commit, and estimate how long each branch lived. What would a branch that lived for a month have done to its conflicts?

When you are finished, delete the lab: cd .. && rm -rf branching-lab.

11. Review prompt

From memory, sketch the graph that results from a fast-forward merge and from a merge commit, using four or five commits each. Then explain in two or three sentences what a correct conflict resolution looks like and when you would choose rebase over merge.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check