GitHub collaboration: remotes, push and pull, forks, pull requests, reviews and issues
Connect a local repository to GitHub, push, fetch and pull with real output, contribute through forks and pull requests, review code, track work with issues and Projects, and protect main with rulesets.
Key ideas
How it works
1. Push to GitHub
After creating an empty repository on GitHub, add it as origin and push. A local bare repository stands in for GitHub here so you can see real output; with GitHub the URL looks like [email protected]:you/notes.git.
$ git remote add origin ../github/notes.git
$ git remote -v
origin ../github/notes.git (fetch)
origin ../github/notes.git (push)
$ git push -u origin main
branch 'main' set up to track 'origin/main'.
To ../github/notes.git
* [new branch] main -> main
$ git branch -vv
* main 209b3bc [origin/main] Add glossary2. Get a teammate's work: fetch, then pull
$ git clone -q github/notes.git ravi && echo cloned
cloned
$ git config user.name "Ravi Teammate" && git config user.email [email protected] && echo 'Q3: What is a remote?' >> quiz.md && git commit -q -am "Add quiz question 3" && git push -q && git log --oneline -1
addeeb7 Add quiz question 3
$ git fetch
From ../github/notes
209b3bc..addeeb7 main -> origin/main
$ git status -sb
## main...origin/main [behind 1]
$ git log --oneline main..origin/main
addeeb7 Add quiz question 3
$ git pull --ff-only
Updating 209b3bc..addeeb7
Fast-forward
quiz.md | 1 +
1 file changed, 1 insertion(+)git fetch downloads safely without changing your files; git status -sb shows you are behind; git pull --ff-only updates only if no merge is needed, which avoids surprise merge commits.
3. The pull request workflow
- Create a branch:
git switch -c fix/typo-in-readme. - Commit your change and push the branch:
git push -u origin fix/typo-in-readme. - On GitHub select Compare & pull request (or run
gh pr create --fill). Write what changed, why, and how you tested it. Link the issue with Fixes #12. - Reviewers comment line by line, suggest changes, then Approve or Request changes. Automated checks (GitHub Actions) run on every push.
- Push more commits to the same branch to address feedback; the PR updates automatically.
- Merge with the method your team uses, delete the branch, and run
git switch main && git pull.
4. Contribute to someone else's project
- Select Fork on the project's GitHub page to make your copy.
- Clone your fork, and add the original as
upstream:git remote add upstream https://github.com/owner/project.git. - Keep in sync:
git fetch upstreamthengit merge upstream/main(or use Sync fork on GitHub). - Branch, commit, push to your fork, and open a pull request to the original repository. Read its CONTRIBUTING.md first.
5. Good reviews and good pull requests
| Author | Reviewer |
|---|---|
| Small PRs (under ~300 lines changed) with a clear title | Review within a day; start with the description and tests |
| Explain why, link the issue, add screenshots for UI | Ask questions rather than demand; separate must-fix from nice-to-have |
| Run tests and lint before asking | Use suggested changes for small fixes the author can accept in one click |
6. Issues, Projects and protecting main
- Issues: one per bug or task, with labels (bug, enhancement, good first issue) and an assignee. Templates help reporters give the right details.
- Projects: boards and tables that track issues and PRs across repositories, with custom fields such as priority and sprint.
- Rulesets () or branch protection: require pull requests, at least one approval, passing status checks and no force pushes on main.
- CODEOWNERS: a file that automatically requests reviews from the right people for each folder.
Common mistakes and fixes
| Mistake | Fix |
|---|---|
| git pull creating messy merge commits | Fetch first, or use git pull --ff-only; rebase your own branch on main when needed. |
| Huge pull requests | Split work into small PRs that each do one thing. |
| Pushing directly to main | Protect main with a ruleset that requires pull requests and checks. |
Practice
- Push a project to GitHub and open a pull request from a branch, even if you review it yourself.
- Fork a public repository, sync it with upstream, and open a small documentation PR.
- Create a ruleset on your own repository that requires a pull request for main.
Quick check
What is the difference between fetch and pull?
fetch downloads commits without changing your files; pull fetches and then merges (or rebases) into your branch.
Why add an upstream remote to a fork?
To fetch new commits from the original project and keep your fork in sync.
What does "Fixes #12" in a PR description do?
It links the PR to issue 12 and closes the issue when the PR is merged into the default branch.