A pull request on a real server
A team keeps its code on a server such as Gitea or GitHub. Nobody changes the main branch directly. You push a branch, open a pull request, and a colleague reads it first.
In this lesson you do the whole thing against the lab's Gitea, from a push to a merge.
By the end of this lesson
Note
I can push a branch, open a pull request on a server and merge it.
Key words
| Word | What it means |
|---|---|
| Pull request | a request to merge a branch, with a page to review it |
| API | a way for a program to ask a server to do something |
| Mergeable | able to merge with no conflict |
| Merge commit | a commit with two parents that joins two branches |
| Upstream | the remote branch your branch is linked to |
The cells students fill in are left blank in this preview.
A branch must be on the server first
A pull request needs the branch to exist on the server, so you push it. Git cannot open the pull request for you.
First make a repository on Gitea. A program can do that by calling its API. The -u of git push links your branch to its remote twin, the upstream. The lab's password is public, so it is safe to type in an address here. Never do that with a real password.
Make a repository on Gitea
curl -s -o /dev/null -w '%{http_code}\n' -u student:student -H 'Content-Type: application/json' -d '{"name":"menu-review","auto_init":true,"default_branch":"main"}' http://gitea:3000/api/v1/user/reposShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Clone it
git clone http://student:student@gitea:3000/student/menu-review.gitShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Start a branch for the change
git -C menu-review switch -c tea-priceShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Add a line to the README
printf 'Tea costs 2.00 from Monday.\n' >> menu-review/README.mdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit the change
git -C menu-review commit -am "Say what tea costs"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Push the branch
git -C menu-review push -u origin tea-priceShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Open the pull request, then review it
Press New Pull Request on the repository page, or ask the API, which is what the button does. The server then works out whether the branch is mergeable.
A reviewer reads the diff and writes comments. Gitea will not let you approve your own change, so here you comment. In a team a colleague approves.
Open the pull request
curl -s -o /dev/null -w '%{http_code}\n' -u student:student -H 'Content-Type: application/json' -d '{"title":"Say what tea costs","head":"tea-price","base":"main","body":"Tea costs 2.00 from Monday."}' http://gitea:3000/api/v1/repos/student/menu-review/pullsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Ask if it can merge cleanly
curl -s http://gitea:3000/api/v1/repos/student/menu-review/pulls/1 | grep -o '"mergeable":[a-z]*'Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Write a review comment
curl -s -o /dev/null -w '%{http_code}\n' -u student:student -H 'Content-Type: application/json' -d '{"body":"Looks right to me."}' http://gitea:3000/api/v1/repos/student/menu-review/issues/1/commentsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the diff as a reviewer
curl -s http://gitea:3000/api/v1/repos/student/menu-review/pulls/1.diffShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Merging happens on the server
When the review is done, someone presses Merge. The server makes a merge commit on main. Your own computer knows nothing about it yet.
So you run git fetch, which downloads what is new without changing your files. Then you read the history of origin/main, the copy Git keeps of the server's branch.
Merge the pull request
curl -s -o /dev/null -w '%{http_code}\n' -u student:student -H 'Content-Type: application/json' -d '{"Do":"merge"}' http://gitea:3000/api/v1/repos/student/menu-review/pulls/1/mergeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Download what is new
git -C menu-review fetchShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Draw the history of main on the server
git -C menu-review --no-pager log --graph --format=%s origin/mainShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think that merging a pull request changes the files on their own computer. It does not. The merge happens on the server. Run git fetch to see it. Then switch to main and run git pull to bring it into your own main.