Learn / Programming / Git & Command Line / Remotes, GitHub and Pull Requests

Intermediate 15 min

Remotes, GitHub and Pull Requests

Push, pull, fork and review code with pull requests.

What you will learn

  • Push and pull
  • Open a pull request
  • Keep a fork up to date

So far everything lived on your machine. A remote is a copy of the repository hosted elsewhere, usually on GitHub, GitLab or Bitbucket. It is how you back up work, share it and collaborate.

git clone https://github.com/user/repo.git        # copy an existing repo

# or connect an existing local repo
git remote add origin git@github.com:user/repo.git
git remote -v
git push -u origin main       # first push; -u remembers the link

origin is just the conventional name of your main remote. Use SSH keys (or a credential helper) so you are not typing passwords.

git push                 # upload my commits
git fetch                # download others' commits without touching my files
git pull                 # fetch + merge (or rebase) into my branch
git pull --rebase        # keep history linear

If a push is rejected with "non-fast-forward", someone else pushed first. Pull, resolve anything needed, and push again.

The pull request workflow

  • 1. Update main (git pull) and branch off it.
  • 2. Commit your work, then git push -u origin feature/x.
  • 3. On GitHub, open a pull request (PR) from your branch into main.
  • 4. Teammates review, comment and request changes; automated checks (CI) run.
  • 5. Push more commits to the same branch to update the PR. When approved, merge it.
  • 6. Delete the branch and pull the updated main.

A good PR is small, has a clear title and description (what and why), and links the issue it solves. Reviewers can then respond in minutes instead of hours.

Forks

For open source projects you cannot push to, fork the repo (your own copy on GitHub), clone your fork, and open PRs from it. Keep it updated by adding the original as a second remote:

git remote add upstream https://github.com/original/repo.git
git fetch upstream
git switch main
git merge upstream/main
git push origin main
gh auth login
gh pr create --fill              # open a PR from the current branch
gh pr checkout 123              # try someone else's PR locally
gh pr merge --squash
Protect main

Turn on branch protection: require PRs, reviews and passing checks before merging. It prevents accidents.

Try it yourself

Create a repo on GitHub, clone it, create a branch, push a change and open a pull request from it.

Show solution
git clone git@github.com:you/practice.git && cd practice
git switch -c docs/add-notes
echo "notes" > NOTES.md && git add NOTES.md && git commit -m "Add notes"
git push -u origin docs/add-notes
gh pr create --fill