Sub topic 8
Push the Branch to Your Fork
This chapter assumes you have a fork cloned locally, with origin pointing to your fork and upstream pointing to the original repository. It also assumes you have made at least one focused commit on a feature branch. Pushing is the step that copies that work from your machine to your fork on GitHub, which is where a pull request is opened from.
Why you push to origin
You do not have write access to the original repository, so you cannot push there. Your fork is the copy you own, and origin is the remote that points to it. A pull request asks the upstream maintainers to pull commits from a branch on your fork.
Push the branch
From the feature branch, run:
git switch fix-readme-typo
git push -u origin fix-readme-typoThe command has three parts:
originnames the remote, which is your fork.fix-readme-typonames the branch to send. Git creates a branch with the same name on the fork if one does not exist.-u(short for--set-upstream) records that your local branch tracksorigin/fix-readme-typo.
Because of that tracking link, later you can run git push or git pull on this branch without naming a remote or branch.
Git prints a summary of the upload. It usually includes a link for creating a pull request. Keep that link, because the next chapter uses it.
Check that the push worked
Confirm the result in two ways.
- Run
git status. It should report that your branch is up to date withorigin/fix-readme-typo. - Open your fork on GitHub. A banner such as "fix-readme-typo had recent pushes" appears, and the branch is listed in the branch dropdown.
You can also list tracking details locally:
git branch -vvThe output shows each local branch with the remote branch it tracks.
Common errors
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `Permission denied` or `403` | `origin` points to the original repository, or your credentials are wrong | Run `git remote -v`. Reset with `git remote set-url origin https://github.com/YOUR-USERNAME/REPO.git`. Then check your token or SSH key. |
| `The current branch has no upstream branch` | You ran `git push` without `-u` on a new branch | Run `git push -u origin BRANCH-NAME`. |
| `src refspec ... does not match any` | The branch name is misspelled, or you have no commits yet | Check `git branch` and `git log`. |
| `rejected ... non-fast-forward` | The fork's branch has commits your local branch lacks | Run `git fetch origin`, inspect the difference, then integrate it. |
Pushing more commits
After the first push, adding work is short. Commit as usual, then push:
git add README.md
git commit -m "Clarify install command"
git pushIf a pull request is already open from this branch, the new commit appears in it automatically. You do not need to open a new one.
Exercise
You created the branch add-docs-link, committed once, and ran git push with no arguments. Git says the current branch has no upstream branch. What command fixes this, and what does the -u option do?
Show answer
Run git push -u origin add-docs-link. The -u option pushes the branch to origin (your fork) and records that the local branch tracks origin/add-docs-link, so later git push and git pull work without extra arguments.