Git Branches and Pull Requests: How Teams Work Together

The feature branch workflow step by step: create a branch, commit, push, open a pull request, handle review, merge and fix a conflict, with real Git output.

AAyim Nelson27 Nov 20269 min read5

Ama, Kofi and Esi are building their school's results website in Kumasi. At first they took turns: Ama edited the files, zipped them up and sent them on WhatsApp, and Kofi carried on from there. It worked until two of them changed the same file on the same evening, and someone's work quietly disappeared.

Real software teams solve this with Git branches and pull requests. Everyone works on their own branch, a teammate checks the change, and only then does it join the main code. This post walks through that workflow step by step, with the real Git commands and the real output they print.

What a branch is

A Git repository keeps your project's history as a chain of commits, snapshots of your files. A branch is a named line of work in that history. Every repository starts with one branch, usually called main, and on a team main is the version that should always work.

When you start a new feature, you create a branch from main and commit on it. Your commits stay on your branch, so main (and everyone else's work) is untouched until your change is ready. Under the hood a branch is just a label that points at a commit, so creating one is instant and costs nothing.

The feature branch workflow

Most teams on GitHub follow the same steps:

  1. Get the latest main.
  2. Create a branch for one feature or fix.
  3. Commit your work on the branch.
  4. Push the branch to GitHub.
  5. Open a pull request asking to merge it into main.
  6. A teammate reviews it; you push fixes until it's approved.
  7. Merge it, then delete the branch.

Let's follow Kofi as he adds a grade key to the results page. A note on the output below: to run everything on one computer, the team's shared copy is a folder that plays the part of GitHub, so the To and From lines show a folder path. I've shortened it to C:/Users/you/...; with GitHub you'd see a https://github.com/... address instead. Your commit numbers (like 620d71b) will be different too.

Step 1: Start from an up-to-date main

git switch main
git pull
Already on 'main'
Your branch is up to date with 'origin/main'.
Already up to date.

Starting from the newest main means your branch includes everyone else's finished work, which keeps surprises small later.

Step 2: Create a branch

git switch -c creates a branch and moves you onto it in one go:

git switch -c add-grade-key
Switched to a new branch 'add-grade-key'

Name branches after the work, in lower case with hyphens: add-grade-key, fix-login-error. Some teams add a prefix such as feature/. One branch should do one thing.

Step 3: Make the change and commit it

Kofi adds a line to results.html. Before committing, he checks what Git sees:

git status
On branch add-grade-key
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:   results.html

no changes added to commit (use "git add" and/or "git commit -a")

And exactly what changed, with git diff. Lines starting with + are new:

diff --git a/results.html b/results.html
index 177ceee..188ada5 100644
--- a/results.html
+++ b/results.html
@@ -1,2 +1,3 @@
 <h1>Your results</h1>
 <table id="results"></table>
+<p>A1 = Excellent, B2 = Very good, B3 = Good</p>

Happy with it, he commits. -a stages every tracked file that changed, and -m gives the message:

git commit -am "Add grade key to results page"
[add-grade-key 620d71b] Add grade key to results page
 1 file changed, 1 insertion(+)

On Windows you might also see a warning that "LF will be replaced by CRLF". That's about line endings, and it's harmless.

Step 4: Push the branch

So far the branch lives only on Kofi's laptop. He pushes it to GitHub. The first time, -u links the local branch to the one on GitHub, so later he can just type git push:

git push -u origin add-grade-key
To C:/Users/you/.../results-site.git
 * [new branch]      add-grade-key -> add-grade-key
branch 'add-grade-key' set up to track 'origin/add-grade-key'.

When you push a new branch to GitHub, it also prints a link for opening a pull request for it.

Step 5: Open a pull request

A pull request (PR) is a page on GitHub that says: "I'd like to merge this branch into main. Please check it." Kofi clicks Compare & pull request and fills it in. A good PR has:

  • A clear title, like a commit message: "Add grade key to results page".
  • A description: what changed, why, and how to check it. For a page, a screenshot helps.
  • A reviewer: Kofi asks Esi.
  • A small size. A short PR gets a careful review; a huge one gets a quick "looks fine" and hides bugs.

The PR shows every commit and every changed line. If you want early feedback on unfinished work, open it as a draft pull request.

Step 6: Review, then update the same PR

Esi reads the change and leaves a comment: "Could the grade key be a list? It would read better on a phone." Good reviews are about the code, never the person. Ask rather than order, explain why, mark small points as optional (many teams start them with "Nit:"), and say what you like too. On GitHub, a review ends with Approve, Comment or Request changes.

Kofi doesn't need a new pull request. He edits the file, commits on the same branch and pushes again:

git commit -am "Show grade key as a list"
git push
[add-grade-key 76da25b] Show grade key as a list
 1 file changed, 5 insertions(+), 1 deletion(-)
To C:/Users/you/.../results-site.git
   620d71b..76da25b  add-grade-key -> add-grade-key

The pull request updates by itself with the new commit. Esi checks it and approves.

Step 7: Merge and tidy up

Esi clicks Merge pull request on GitHub, then Delete branch. GitHub makes a merge commit on main. Kofi brings his laptop up to date and deletes his local copy of the branch:

git switch main
git pull
git branch -d add-grade-key
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
From C:/Users/you/.../results-site
   271b67e..9329e45  main       -> origin/main
Updating 271b67e..9329e45
Fast-forward
 results.html | 5 +++++
 1 file changed, 5 insertions(+)
Deleted branch add-grade-key (was 76da25b).

git branch -d only deletes a branch whose work is safely merged, so you can't lose commits by accident. Now look at the history as a graph:

git log --oneline --graph
*   9329e45 Merge pull request #1 from kumasi-coders/add-grade-key
|\
| * 76da25b Show grade key as a list
| * 620d71b Add grade key to results page
|/
* 271b67e Add home and results pages

You can see the branch leave main, collect two commits, and join back in. GitHub can also squash and merge (all the PR's commits become one) or rebase and merge. Teams pick one style and stick to it.

When two branches change the same line

Most merges happen on their own. But if two branches change the same lines of a file, Git can't guess which version is right. Here, one branch reworded the heading and main changed it differently:

Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Git writes both versions into the file between markers:

<<<<<<< HEAD
<h1>Adum Hill SHS: Second Term Results</h1>
=======
<h1>Adum Hill Senior High School Results</h1>
>>>>>>> new-heading
<p>Check your end-of-term results here.</p>

The part under HEAD is the branch you're on; the part under ======= is the branch you're merging. Edit the file so it says what it should (often a mix of both), delete the three marker lines, then git add index.html and git commit. If you'd rather stop and think, git merge --abort puts everything back as it was. On GitHub, a PR with a conflict shows a warning and won't merge until it's resolved.

Protect main

A workflow only helps if everyone follows it. In the repository's settings on GitHub, a branch protection rule (or a ruleset) for main can require a pull request and an approval before anything is merged. Then a quick "I'll just push this typo fix to main" is refused, and the fix goes through a PR like everything else.

Habits that keep teams happy

  • Keep branches short. Merge in a day or two, not weeks, so conflicts stay small.
  • Pull main often, and bring it into your branch, so you see conflicts early.
  • One branch, one job. A fix and a new feature are two pull requests.
  • Review promptly. A PR waiting three days blocks a teammate.
  • Write messages for the next person. "Fix grade rounding for 49.5" beats "update".

Learn Git on JeliScript

Branches and pull requests are how almost every software team works, and employers expect you to know them. The Git and GitHub for Teams course follows Ama, Kofi and Esi from their first commit to merge conflicts, a rejected push, pull requests, code review, a protected main branch, a tagged release and an automatic test run, with every Git command really run.

Git is one step on the way to building complete web apps. The Full-Stack Developer roadmap shows where it fits, alongside HTML, CSS and JavaScript, Python and Django, REST APIs, React, databases, testing and deployment.

Comments

Log in to join the conversation.

Loading comments...