Sub topic 3
Opening a pull request
This chapter assumes you can commit, branch, and push with Git, and that you have a GitHub repository you can push to. It covers how to open a pull request (PR) from a feature branch, in the web interface and with the gh CLI. It also covers draft pull requests.
Push a branch first
GitHub can only compare branches that exist on the remote. Create a branch, commit, and push it with an upstream link:
git switch -c fix-login-timeout
# edit files, then:
git add .
git commit -m "Increase login request timeout to 30s"
git push -u origin fix-login-timeoutThe -u flag links your local branch to origin/fix-login-timeout. Later git push calls then need no arguments.
Choose the base and head
Every pull request has two branches:
- The base branch is where the change will land, usually
main. - The head branch is the branch that holds your commits.
The PR shows the difference between them. If the base moves forward while you work, GitHub may report a conflict, which a later chapter covers.
Open it in the web interface
After a push, GitHub often shows a "Compare & pull request" banner on the repository page. You can also use the Pull requests tab and choose New pull request. Check the base and head branches, write a title and description, and click Create pull request.
Open it with the gh CLI
The GitHub CLI creates the same pull request without leaving the terminal:
gh pr create --base main --title "Increase login request timeout" --body "Raises the timeout from 10s to 30s to stop spurious failures on slow networks."
gh pr create --draft --fillThe first command sets everything explicitly. The second opens a draft and fills the title and body from your commits. With --fill, a single-commit branch uses that commit's subject and body. A branch with several commits uses the branch name for the title.
Draft pull requests
A draft pull request signals that the work is not ready for formal review. Use one to:
- Get early feedback on direction.
- Run CI before the code is finished.
- Share progress without notifying reviewers to approve.
A draft cannot be merged. When you are ready, click Ready for review on the page, or run gh pr ready. Marking it ready triggers review requests, including automatic ones from CODEOWNERS.
What happens next
Once the PR exists, it is a conversation attached to two branches. New commits you push to the head branch appear in the PR automatically.
Common errors
- No "Compare" banner appears. The branch was not pushed, or the push went to another remote. Check with
git branch -vv. - The diff shows unrelated commits. You branched from an outdated base or chose the wrong base branch. Check the base dropdown first.
gh pr createasks which repository to use. The repository has several remotes, such as a fork and its upstream. Pass--repoor set a default withgh repo set-default.
Exercise
You pushed a branch with half-finished work and want CI results and early feedback, but you do not want anyone to approve or merge it yet. Which gh command opens the PR, and which command marks it ready later?
Show answer
Open it with gh pr create --draft --fill. When the work is finished, run gh pr ready. A draft pull request cannot be merged, and marking it ready starts the formal review.