Branches, Merging & Pull Requests
How Git branches let you work in parallel, and how GitHub pull requests turn a merge into a reviewed conversation.
What you'll learn
- Explain what a branch is and why teams use them
- Describe what happens when two branches are merged
- Walk through the GitHub pull-request workflow from branch to merge
- Recognize what causes a merge conflict
Prerequisites
Explanation
A branch is a movable pointer to a commit. Every repository starts with one branch (conventionally called main), but nothing stops you from creating another pointer that starts at the same commit and then diverges as you make new commits on it — while main stays untouched. This is how one project supports many people (or one person juggling many ideas) working at the same time without stepping on each other.
Why branch instead of always committing to main? Because main usually represents "the version that works." A feature branch, like feature/login-page, is a safe sandbox: you can commit half-finished, experimental work there, and main never sees it until you're ready.
Merging is how work travels back. When you merge a feature branch into main, Git combines the two histories: every commit made on the feature branch becomes part of main's history too. If nothing on main changed while you were away, Git can often just move the main pointer forward (a "fast-forward"). If both branches changed different things, Git creates a new merge commit that has two parents, weaving the histories together automatically.
Merge conflicts happen when both branches changed the same lines of the same file in different ways. Git can't guess which version you want, so it pauses and asks a human to pick (or combine) the correct result. A conflict is not a sign anything is broken — it's Git correctly refusing to guess.
The GitHub pull request (PR) workflow builds a review conversation on top of branching:
- You create a branch and push commits to it on GitHub.
- You open a pull request, which compares your branch against
mainand shows the exact diff. - Teammates read the diff, leave comments, and request changes if needed.
- Once approved, someone clicks Merge — GitHub performs the actual Git merge for you.
- The feature branch is typically deleted afterward, since its work now lives on
main.
A pull request itself is not a Git concept — Git only knows about branches, commits, and merges. The "pull request" is a feature GitHub (and similar platforms) layers on top, giving teams a structured place to discuss a change before it becomes permanent.
A feature branch merging back into main
main: commit 1 → commit 2 → commit 3 → (merge commit 6). feature/login-page branches off commit 2, adds commit 4 → commit 5, then a pull request merges commits 4 and 5 into main as merge commit 6, after which main contains all six commits' work.
Guided local lab
Create a Feature Branch, Make Commits, and Merge It Back
Runs on your computerPractice the real branch-then-commit-then-merge workflow in a disposable local repository, including seeing a fast-forward merge and reading `git log --graph` -- every command below runs in YOUR terminal; this platform does not execute any of them.
Required tools
- Git (2.x or newer)
- A terminal (or Git Bash on Windows) (any current version)
Setup
- Open a terminal.
- Create a fresh practice repository: `mkdir git-branching-practice && cd git-branching-practice && git init`.
- Make one commit on main so there's a starting point to branch from: `echo "# Branching Practice" > README.md && git add README.md && git commit -m "Initial commit"`.
Project structure
git-branching-practice/ .git/ README.md nav.md (added on the feature branch)
Starter files
README.md
TODO: create this yourself with the setup command above.
Requirements
- A branch named feature/nav-bar exists, created from main.
- At least two commits exist on feature/nav-bar that do not exist on main.
- feature/nav-bar is merged back into main.
- `git log --oneline --graph --all` shows both the branch point and the merge.
Commands to run
Create and switch to a new branch in one step
git checkout -b feature/nav-barAdd a new file for the feature
printf "- Home\n- About\n" > nav.mdStage and commit it
git add nav.md && git commit -m "Add nav-bar markup"Make a second commit on the same branch
printf "- Contact\n" >> nav.md && git add nav.md && git commit -m "Add Contact link to nav-bar"Switch back to main
git checkout mainMerge the feature branch into main
git merge feature/nav-barView the branch and merge in the commit graph
git log --oneline --graph --allDelete the now-merged branch (safe: git refuses if it isn't fully merged)
git branch -d feature/nav-bar
Expected behavior
Because nothing changed on main while feature/nav-bar was being worked on, `git merge feature/nav-bar` performs a fast-forward: main's pointer simply moves forward to the branch's latest commit, with no separate merge commit. `git log --oneline --graph --all` shows a single straight line of commits, not a diverging-then-rejoining shape -- that straight line is exactly what a fast-forward merge looks like.
Verify it yourself
git branchExpected: lists only 'main' (feature/nav-bar was deleted in the last command, since it was safely merged first)
cat nav.mdExpected: prints all three lines (Home, About, Contact) -- confirming both feature-branch commits are now part of main
git log --onelineExpected: shows all commits from both the initial commit and the two feature-branch commits, in one combined history
Troubleshooting
- `error: branch 'feature/nav-bar' is not fully merged` — This means `git branch -d` correctly refused to delete a branch with unmerged work -- go back and confirm `git merge feature/nav-bar` actually completed successfully first.
- `git merge` opens a text editor for a merge commit message — This only happens for a real (non-fast-forward) merge, which needs a message describing the merge itself -- save and close the editor to accept the default message, exactly like `git commit` without -m.
- Merge conflict markers (<<<<<<<, =======, >>>>>>>) appear in a file — This lab is designed to avoid a real conflict, but if you experiment further and hit one: open the file, manually keep the correct lines, delete the marker lines themselves, then `git add` the file and `git commit` to complete the merge -- never force through a conflict without reading what both sides changed.
Stuck? Get a hint.
Extension challenge
Before merging, make a NEW commit directly on main (e.g. edit README.md and commit it) so main and feature/nav-bar have both diverged -- then merge feature/nav-bar and compare: this time Git creates a real merge commit with two parents, and `git log --graph` shows the branches actually diverging and rejoining instead of one straight line.
When you've verified this locally, use the "Mark lesson complete" button below to record your progress.
Guided lab
Guided walkthrough: branch, commit, and fast-forward merge
Follow each step to see exactly what Git prints when creating a branch, committing on it, and merging it back -- this reads through the same commands as the guided local lab above; try them for real there when you're ready.
Step 1 of 5
git checkout -b creates a new branch and switches to it in one step.
git checkout -b feature/nav-barStuck? Get a hint.
Common mistakes
- Believing that merging automatically deletes the original branch — it doesn't, unless you delete it yourself afterward.
- Assuming a pull request is a core Git feature — it's a workflow built by platforms like GitHub on top of Git branches.
- Panicking at a merge conflict — it only means Git needs a human decision about conflicting lines, not that something broke.
- Committing directly to main on a team project instead of opening a feature branch and a pull request.
Knowledge check
Takeaway
Branches let work happen in parallel without disturbing main, and pull requests turn the act of merging into a reviewed, collaborative conversation.
Summary
A branch is a pointer to a commit that lets you diverge from main safely; merging reunites two histories, occasionally producing a conflict a human must resolve. GitHub's pull-request workflow layers review and discussion on top of that merge.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.