Skip to content
Ameen Abozaid
PublishedBEG-047-L05

Demo: publishing a project and reviewing a pull request

  • Lesson 5 of 5
  • 11 min read
  • Beginner
On This Lesson 11 topics

Lesson Overview

This is a hands-on lesson. You will take a small Java project from your computer to a public GitHub repository, give it a README, and run one complete pull request round with a peer: branch, push, open the PR, receive a comment, push a fix, merge. Along the way you will make one planned mistake, committing to main by accident, and learn two ways to undo it.

The lesson prepares the module lab (BEG-047-LAB1: publish two projects with READMEs, and open a PR that a peer reviews). Everything here uses the four earlier lessons: remotes and push, authentication, pull requests, and Markdown. If you have not set up authentication yet, do the previous lesson on it first.

Commands are for a terminal, because editor and IDE menus differ. GitHub's pages also change over time, so names of buttons below are descriptive: look for the matching label. The command output shown was produced with Git 2.44 and JDK 21 against a local stand-in for GitHub. Your hashes and addresses will differ.

Learning Objectives

By the end of this lesson you will be able to:

  • Create an empty GitHub repository and publish a local project to it.
  • Add a README with setup and usage and test it by running its commands.
  • Run a full PR round: branch, push, open, review comment, fix, merge.
  • Update your local main after the merge and delete the finished branch.
  • Recover from an accidental commit on main, both before and after pushing it.
  • Check the lab's acceptance rules: public repositories, and a PR with at least one review round.

Written Explanation

The plan

You need a project to publish. Any small Java project works: Bank Accounts from earlier, or a one-file program. Here we use a single Main.java that prints one line, so the focus stays on Git and GitHub. The steps:

  1. Create an empty repository on GitHub and publish main.
  2. Add a README.md with setup and usage.
  3. Create a branch, change one line, push it, and open a PR.
  4. A peer leaves a comment; push a fix to the same branch.
  5. Planned mistake: commit to main by accident, and undo it.
  6. Merge the PR and update your computer.

Keep a second goal in mind: nothing secret. Before every git add, look at git status. A public repository is readable by anyone.

Why a peer and not yourself

A review is only useful if someone else reads the change; reviewing your own work defeats the purpose. Pair up with a classmate or friend with a GitHub account and add them as a collaborator (in the repository settings, under the section for collaborators; the exact name may vary), or ask them to fork your repo and comment from there. For a public repository, anyone with a GitHub account can leave a comment on your PR, so a peer who is not a collaborator can still review by commenting. Whether a non-collaborator's "approve" counts toward a required approval depends on the repository rules, so do not worry about that for the lab.

Examples

Example 1: create the repository and publish main (simple)

On GitHub, create a new repository named bank-accounts, set it to Public, and leave "add a README", ".gitignore", and "license" unticked, so it starts empty. Then locally:

bash
git status
git remote add origin https://github.com/you/bank-accounts.git
git remote -v
git push -u origin main

(Use the SSH address instead if you set up SSH.) A successful push prints something like:

text
branch 'main' set up to track 'origin/main'.
To https://github.com/you/bank-accounts.git
 * [new branch]      main -> main

Reload the repository page on GitHub: your files and commits are there. If you see "failed to push" or an authentication error, return to the authentication lesson. If your branch is not called main (older Git versions used another default name), check git branch and use the real name.

Example 2: add a README, publish it, and test it (practical)

Create README.md in the project root:

markdown
# Bank Accounts

Console app.

## Setup

Requires Java 21.

```bash
javac Main.java
```

## Usage

`java Mian`

(That Mian typo is deliberate: it is the change a reviewer will help you find.) Commit and push it straight to main this one time, because there is nothing to protect yet and no reviewer set up:

bash
git add README.md
git commit -m "Add README"
git push

Now test your README as a stranger: run only its commands.

bash
javac Main.java
java Mian

The second command fails, because the class is Main, not Mian. This is exactly the kind of mistake a README test finds. We will fix it on a branch, with a PR.

Example 3: the PR round, with a planned mistake (realistic)

Step 1: branch and fix.

bash
git switch -c readme-fix
# correct java Mian to java Main in README.md
git commit -am "Fix run command in README"
git push -u origin readme-fix
text
branch 'readme-fix' set up to track 'origin/readme-fix'.
To https://github.com/you/bank-accounts.git
 * [new branch]      readme-fix -> readme-fix

Step 2: open the PR. On GitHub, open the "Compare & pull request" banner for readme-fix. Base: main. Title: "Fix run command in README". Description, with what, why, and how to check: "What: java Mian becomes java Main. Why: the command failed. How to check: follow the Setup and Usage sections in a fresh clone." Create it, and send the link to your peer.

Step 3: the review. Your peer opens Files changed, comments on a line in the diff ("Please also show what the program prints"), and submits the review as a comment or request changes. You answer by editing the file on the same branch:

bash
# add "Expected output: Bank accounts ready" to the Usage section
git commit -am "Show expected output"
git push
text
   ee03114..ec7c262  readme-fix -> readme-fix

No new PR was needed: the new commit appears on the same PR page. Reply to the comment and ask your peer to review again and approve.

Step 4: the planned mistake. While waiting you are on main, you make "a tiny tweak", and you commit on main straight away, because your hands know git commit -am:

bash
git switch main
# edit README.md
git commit -am "Quick tweak"
git status -sb
text
## main...origin/main [ahead 1]

ahead 1 is the alarm: main has a commit that GitHub's main lacks and that no one reviewed. There are two situations.

Not pushed yet. Save the commit on a branch, then move main back to match the remote:

bash
git branch rescue
git reset --hard origin/main
git status -sb
text
HEAD is now at d583ca2 Add README
## main...origin/main

The work now lives on rescue, ready to push as a proper PR. git reset --hard discards uncommitted changes and moves the branch, so only use it after saving the commit on a branch, and only for commits you have not pushed.

Already pushed. Do not rewrite the shared history. Add a new commit that undoes the old one with git revert:

bash
git revert --no-edit HEAD
git push
text
[main 1be1905] Revert "Quick tweak 2"
 1 file changed, 1 deletion(-)

The history keeps both commits, and the revert cancels the change. It is honest and safe, because nobody who pulled the earlier commit has their history changed. Both fixes show why teams protect main: with a branch protection rule requiring a PR, the accidental push would have been refused. If your own repository allows it, switch on a rule for main in the repository settings afterwards and try the mistake again: the push should be rejected. (The setting's exact menu location may change; search GitHub's documentation for "protected branches".)

Step 5: merge. Once the PR is approved, press the merge button, then update your computer:

bash
git switch main
git pull
git branch -d readme-fix

Run the README commands one last time in a fresh clone: javac Main.java, then java Main. The output should be Bank accounts ready. In the scratch run I compiled and ran this one-line program under JDK 21, and that was the output.

Backend Insight

The sequence in this demo is the daily loop of almost every backend team: pull the latest main, branch, commit, push, open a PR, answer review comments, merge, repeat. The commands are the same whether the change is one README line or a new endpoint.

The planned mistake is not rare. Even experienced developers sometimes commit on the wrong branch. What differs is the recovery: they check git status before pushing, notice ahead 1 on main, and fix it before anyone else is affected. When it was already pushed, they use git revert, not a forced push, because a forced push can erase colleagues' work. Looking at git status -sb before you push is a ten-second habit that prevents most problems.

Common Mistakes

Mistake 1: Pushing a branch but never opening the PR

The branch is on GitHub, but nobody knows. Open the PR and share the link.

Mistake 2: Reviewing without reading

A review that just says "looks good" with no looking is empty. Read the diff, and try the README commands.

Mistake 3: Fixing a pushed mistake with a force push

  • Incorrect: git push --force to erase an accidental commit on main.
  • Correct: git revert, then a normal push. It keeps history intact.

Mistake 4: Using git reset --hard without saving the work

If the commit was not on any other branch, it becomes hard to find again. Create a branch first, as shown above.

Mistake 5: Making the repository private by accident (or leaving it empty)

The lab requires public repositories. Open the repository in a private browser window: if you cannot see it, it is not public.

Exercises

Level A: Foundation

A1. Write the commands to connect a local project to https://github.com/you/todo-list.git and publish main.

Show solution A1

A1.

bash
git remote add origin https://github.com/you/todo-list.git
git push -u origin main

A2. After merging a PR on GitHub, which three commands update your computer and clean up the branch fix-typo?

Show solution A2

A2.

bash
git switch main
git pull
git branch -d fix-typo

A3. What does [ahead 1] in git status -sb on main tell you?

Show solution A3

A3. The local main has one commit that the remote's main does not have: it was committed but not pushed. On main, that is an alarm if the commit was not meant to go there.

Level B: Application

B1. You committed on main and have not pushed. Write the exact commands to move the commit to a branch named add-faq and put main back in line with origin/main.

Show solution B1

B1.

bash
git branch add-faq
git reset --hard origin/main

The commit is saved on add-faq. Then git switch add-faq and git push -u origin add-faq to open a PR.

B2. The same commit has already been pushed. What do you do instead, and why?

Show solution B2

B2. Use git revert <commit> (or git revert HEAD for the latest), then git push. Revert adds a new commit that undoes the change without rewriting history others may already have.

B3. List the steps of a PR round from a clean main until the merge, including where a peer's comment fits.

Show solution B3

B3. git switch main, git pull, git switch -c <branch>, commit, git push -u origin <branch>, open the PR, the peer comments on a line in Files changed, you commit the fix and git push to the same branch, the peer approves, you merge, then git switch main, git pull, and delete the local branch.

Level C: Challenge

C1. Plan the lab. Name your two projects, list what each README will contain, and describe how you will do the stranger test for each. Write down how you will show that the PR "has at least one review round".

Show solution C1

C1. Open-ended. A strong plan names two runnable projects, gives each README a description, Requirements, Setup, Usage, and expected output, and uses a different peer or fresh clone to follow only the README. Evidence of a review round: the PR page shows a peer's review comment, your reply or fix commit, and the peer's approval.

C2. Run the planned mistake on a practice repository with branch protection switched on for main (or explain what you expect if the setting is not available). What do you expect the push to do, and what should you do next?

Show solution C2

C2. With a protection rule that requires a PR, the push should be rejected with a message that the branch is protected. Then move your commit to a branch (git branch <name>, then git reset --hard origin/main on main), push the branch, and open a PR. If the setting is not available on your account or repository type, expect the push to succeed and use git revert. The exact message and the availability of the setting depend on your account and GitHub's current features, so I did not test this on GitHub itself.

Mini Challenge

Do the lab's first half now. Pick a second small project, create a public repository for it, publish it, and add a tested README with setup and usage. Then open a PR for one small change in either project and ask a peer to review it.

Check your work: both repositories show in a private browser window, each README works as written in a fresh clone, and the PR page shows at least one review comment from your peer, your reply, and a merge.

Summary & Next Lesson

Key takeaways

  • Publishing: create an empty repository, git remote add origin <url>, git push -u origin main.
  • A README is tested by running its commands, ideally in a fresh clone.
  • A PR round is: branch, push, open, review comment, fix on the same branch, approval, merge, update main.
  • Committed to main by accident? Not pushed: save a branch and git reset --hard origin/main. Pushed: git revert, never a forced push.
  • Check git status -sb before you push; protect main where you can.

Next lesson

This was the last lesson of the module. Finish BEG-047-LAB1 using the steps above: publish two projects with READMEs and complete a PR with at least one review round. The next module in the course specs is not part of this module's materials, so check the course outline for what comes next.

Self-check

  1. Which command publishes main and links it to the remote branch?
  2. What does ahead 1 on main mean?
  3. How do you undo a pushed mistake on main without rewriting history?
  4. After a review comment, where does the fix go?
  5. How do you check that a repository is public?
Show answers
  1. git push -u origin main.
  2. Your local main has one commit that the remote lacks.
  3. git revert, then git push.
  4. As a new commit on the same branch; the PR updates.
  5. Open its page in a private browser window, or while signed out: if you can see it, it is public.

References

  • Git project, Pro Git book, Working with Remotes. Supports: adding a remote, pushing to origin, and rejected pushes.
  • GitHub Docs, Creating a pull request. Supports: the PR creation steps and that later commits on the head branch join the same PR.
  • GitHub Docs, About pull request reviews. Supports: review decisions.
  • GitHub Docs, About protected branches. Supports: required PR reviews and blocked force pushes on protected branches.
  • Not verified from a fetched page: git revert and git reset --hard behaviour was checked only by running Git 2.44 locally. I did not verify whether non-collaborators can review a public repository's PR or whether their approval counts; the lesson hedges this. Collaborator and visibility settings screens were not checked.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.