SkillNavora
Git & GitHub Complete Guide › Lesson 4 of 8 · about 25 minutes
Lesson 4

Branches, merging, conflicts, rebase and stash

Work on features without breaking main: create and switch branches, fast-forward and three-way merges, read and resolve a real merge conflict, rebase a branch on main, and park unfinished work with stash.

Checked against official sources in November 2026. Lessons build on each other.
In plain words. A branch is a parallel draft. You write in your draft while main stays clean. Merging copies your finished draft back. A conflict is Git saying "you and someone else changed the same sentence; you choose the final wording".

Key ideas

git switch -cCreates a new branch and moves to it.
Fast-forward mergeWhen main has no new commits, Git just moves main's pointer to your branch.
Three-way mergeWhen both branches have new commits, Git creates a merge commit joining them.
Merge conflictBoth branches changed the same lines; Git marks them and asks you to decide.
RebaseReplays your branch's commits on top of the latest main, for a straight history.
StashTemporarily shelves uncommitted changes so you can switch tasks.

How it works

1. Branch and fast-forward merge

Real terminal output (captured when this guide was built)
$ git switch -c feature/quiz
Switched to a new branch 'feature/quiz'
$ echo 'Q1: What is a commit?' > quiz.md && git add quiz.md && git commit -q -m "Add quiz question 1"
$ git log --oneline --all
1e3e254 Add quiz question 1
fb1523a Ignore cache files and .env
e26a370 Add Git notes and a hello script
5e9b349 Add README with the course title
$ git switch main
Switched to branch 'main'
$ git merge feature/quiz
Updating fb1523a..1e3e254
Fast-forward
 quiz.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 quiz.md
$ git branch -d feature/quiz
Deleted branch feature/quiz (was 1e3e254).

Because main had no new commits, Git simply moved main forward ("Fast-forward"). No merge commit was needed.

2. A real merge conflict, resolved

Here both branches changed the first line of README.md. Read the markers, write the final version, then add and commit:

Real terminal output (captured when this guide was built)
$ git log --oneline --graph --all -4
* 2117d1a Change title on main
| * 1670ef3 Rename notes title
|/  
* 1e3e254 Add quiz question 1
* fb1523a Ignore cache files and .env
$ git merge feature/title
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
$ git status --short
UU README.md
$ cat README.md
<<<<<<< HEAD
# My Git and GitHub notes
=======
# Asha study notes
>>>>>>> feature/title

## Git
- commit = snapshot
$ printf '# Asha: Git and GitHub notes\n\n## Git\n- commit = snapshot\n' > README.md
$ git add README.md
$ git commit -q -m "Merge feature/title: keep both ideas in the title"
$ git log --oneline --graph -5
*   14bb0e2 Merge feature/title: keep both ideas in the title
|\  
| * 1670ef3 Rename notes title
* | 2117d1a Change title on main
|/  
* 1e3e254 Add quiz question 1
* fb1523a Ignore cache files and .env
MarkerMeaning
<<<<<<< HEADStart of your current branch's version
=======Divider
>>>>>>> feature/titleEnd of the incoming branch's version
  • Edit the file so it reads exactly as it should, and delete all three marker lines.
  • git add the file, then git commit to finish the merge. To give up instead, run git merge --abort.
  • Editors such as VS Code show buttons: Accept Current, Accept Incoming, Accept Both.

3. Rebase for a straight history

Real terminal output (captured when this guide was built)
$ git log --oneline --graph --all -3
* e0492ac Add glossary
| * 6478e23 Add quiz question 2
|/  
*   14bb0e2 Merge feature/title: keep both ideas in the title
|\
$ git switch feature/glossary
Switched to branch 'feature/glossary'
$ git rebase main
Rebasing (1/1)

Successfully rebased and updated refs/heads/feature/glossary.
$ git log --oneline --graph -3
* 209b3bc Add glossary
* 6478e23 Add quiz question 2
*   14bb0e2 Merge feature/title: keep both ideas in the title
|\
$ git switch main && git merge --ff-only feature/glossary
Updating 6478e23..209b3bc
Fast-forward
 glossary.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 glossary.md
Switched to branch 'main'
Careful. Golden rule: rebase only your own branches that nobody else has pulled. Rebasing rewrites commit IDs; never rebase main or a shared branch.

4. Merge or rebase?

MergeRebase
HistoryKeeps exactly what happened, with merge commitsStraight line, easier to read
SafetyNever rewrites commitsRewrites your branch's commits
Use whenJoining shared branches, finishing a pull requestUpdating your own feature branch with the latest main

On GitHub you can choose Create a merge commit, Squash and merge (one commit per pull request) or Rebase and merge for each pull request.

5. Stash unfinished work

Real terminal output (captured when this guide was built)
$ echo 'half-written idea' >> glossary.md
$ git stash push -m "glossary draft"
Saved working directory and index state On main: glossary draft
$ git status --short
$ git stash list
stash@{0}: On main: glossary draft
$ git stash pop
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   glossary.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a9804dbe10f8ba3363df232790ddcdfe5accab07)
$ git status --short
 M glossary.md
$ git restore glossary.md

6. Branch naming that teams like

  • feature/signup-otp, fix/cart-total-rounding, docs/readme-setup.
  • Lowercase, hyphens, short. Include a ticket number if your team uses one: fix/PAY-142-refund-status.
  • Delete branches after merging: git branch -d name locally; GitHub can delete them automatically.

Common mistakes and fixes

MistakeFix
Working directly on mainCreate a branch for every change and merge through a pull request.
Leaving conflict markers in the fileSearch for <<<<<<< before committing; tests should fail if markers remain.
Rebasing a branch others useMerge instead, or agree with the team first.

Practice

  1. Create two branches that change the same line, merge them, and resolve the conflict.
  2. Rebase a feature branch on main and compare git log --graph before and after.
  3. Stash a half-finished change, fix something on main, then pop the stash.

Quick check

When does Git do a fast-forward merge?

When the target branch has no new commits since the feature branch started, so Git just moves the pointer.

How do you finish a merge after fixing a conflict?

git add the fixed files, then git commit (or git merge --abort to cancel).

What is the golden rule of rebasing?

Never rebase commits that other people have already pulled, such as main or shared branches.

What does git stash do?

It shelves uncommitted changes so the working tree is clean; git stash pop brings them back.

Go further (official sources)

Refer & earn