Coding

How Does GitHub Work? Git, Repositories and Pull Requests

GitHub works by hosting your Git repositories in the cloud, so the copy on your computer and the copy on GitHub can be kept in sync with git push and git pull. On top of that hosted copy, GitHub adds pull requests for review, issues for planning and Actions for automation.

Most confusion about how GitHub works comes from treating it as one thing. It is two layers: Git, which runs on your machine, and GitHub, which is a service wrapped around Git repositories. This article walks through each layer, then follows one change from your keyboard to a merged pull request. For a hands-on walkthrough with buttons and commands, see how to use GitHub: a beginner's guide.

Git and GitHub are two different things

Git is a version control system. According to GitHub's documentation, it records changes to files and lets you work on a personal copy of a project and combine your work with other people's safely. Git runs locally and needs no account and no internet connection for everyday work.

GitHub is a cloud platform based on that open-source software. GitHub's own wording is that it hosts your Git projects, called repositories, in the cloud. It then adds planning and collaboration tools around them. Other companies host Git repositories too, which shows the split: the version control comes from Git, and the website, accounts, review tools and automation come from the host.

What is stored where

A Git repository is a project folder plus its full change history. When you work with GitHub you normally have at least two copies of it:

  • The local repository on your computer. Commits, branches and history live here, and you can make commits with no network at all.
  • The remote repository on GitHub. The Pro Git book describes remotes as versions of your project that are hosted on the Internet or network somewhere.

The two copies only change each other when you tell Git to move data between them. Nothing you commit locally appears on GitHub until you push it, and nothing that someone else pushes appears in your working files until you fetch or pull it.

How your computer talks to GitHub: remotes

A remote is a named reference to another copy of your repository. When you clone a repository, Git names the place you cloned from origin. That is only a default shortname, and you can rename it or add more remotes. To see what yours point to, run:

git remote -v

Example output from the Pro Git book, showing a repository cloned over HTTPS:

origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)

The URL can use HTTPS or SSH. The Pro Git book also shows an SSH form, [email protected]:mojombo/grit.git. Which one you pick decides how you prove who you are. If you use SSH, you add a public key to your account, which how to add an SSH key to GitHub covers step by step. To get a first copy onto your machine, see git clone command examples.

The three commands that move changes

CommandWhat it doesChanges your working files?
git push <remote> <branch>Uploads your commits to the remote.No
git fetch <remote>Downloads new data from the remote into your local repository.No
git pullFetches, then merges the remote branch into your current branch.Yes, through the merge

After a fetch, the remote's branches show up locally as <remote>/<branch>, for example origin/main. That is how you can look at incoming work before combining it with yours. The difference between fetch and pull is covered in git fetch vs git pull.

A push can be rejected. The Pro Git book says that if someone else pushed first, your push will be rejected, and you have to fetch their work and incorporate it into yours before you are allowed to push. It also requires write access to the remote. This rule is why GitHub projects settle into a rhythm of small branches that are updated and merged often.

Branches and pull requests: how GitHub organizes teamwork

Pushing straight to the main branch works for a solo project, but teams use branches. You create a branch, commit your changes to it, push the branch to GitHub, then open a pull request. GitHub describes pull requests as proposals to merge code changes into a project, and as its main collaboration feature.

A pull request page is where the review happens. According to GitHub's documentation, it has these parts:

  • The Conversation tab holds discussion and reviews.
  • The Files changed tab shows the diff that reviewers use to understand the proposed changes.
  • The Checks tab shows automated tests and builds.
  • The merge status lists blockers, missing approvals and other requirements before merging.

A draft pull request cannot be merged until you mark it ready for review. Once the requirements are met, a person with permission merges it, and the branch's commits become part of the base branch. How those commits are combined is a separate choice; git merge vs rebase explains the Git side of it.

Forks: contributing to a project you do not own

You cannot push to most public repositories, because you do not have write access. A fork solves this. GitHub defines a fork as a new repository that starts as a copy of another one, the upstream repository. A fork has its own settings and permissions but stays connected to the upstream repository.

A fork is not a branch. A branch is part of one repository, while a fork is a separate repository with its own branches, issues, pull requests and permissions. The usual flow is to fork the project, push your changes to your fork, and open a pull request from your fork back to the upstream repository for the maintainers to review.

Issues and Actions: planning and automation

GitHub's documentation lists issues as the planning tool: you create issues and track your project work with them. They live next to the code, so a pull request and the issue it addresses sit in the same repository.

GitHub Actions is the automation layer. The documentation calls it a CI/CD platform that lets you automate your build, test and deployment pipeline. A workflow is a YAML file checked in to the repository, and it must be in the .github/workflows directory. Events trigger workflows. An event is a specific activity in a repository, such as opening a pull request, creating an issue or pushing a commit. Workflows can also run on a schedule, be started through the REST API, or be run by hand.

This is what the Checks tab on a pull request displays: the results of workflows that ran because you opened or updated the pull request.

One change from start to finish

Putting the pieces together, a typical change on a team project goes like this:

  1. You clone the repository, which creates the local copy and sets the origin remote.
  2. You create a branch and commit your work locally. Nothing is on GitHub yet.
  3. You run git push to upload the branch to GitHub.
  4. You open a pull request. Any workflows configured for pull request events start running.
  5. Reviewers read the diff on the Files changed tab and discuss it on the Conversation tab.
  6. When checks and approvals satisfy the merge status, the pull request is merged into the base branch.
  7. Everyone else runs git pull or git fetch to bring the merged work into their local copies.

Every step in that list is either a Git operation on a local or remote repository, or a GitHub feature layered on top of it. Keeping that distinction in mind makes error messages easier to read. A message about a rejected push is Git talking about the remote. A message about a failed check or a missing approval is GitHub talking about the pull request.

What GitHub does not do

GitHub does not watch your folder. It never sees a change until you commit it and push it, so an edited file that you have not committed exists only on your computer. The same goes in reverse: the website can change files, for example through its in-browser editor, but your local copy stays unchanged until you pull. Public repositories are visible to everyone, so anything pushed to one, including a password or API key, should be treated as published.

Frequently asked questions

Is GitHub the same as Git?
No. Git is the open-source version control system that records changes to files. GitHub is a cloud platform that hosts Git projects, called repositories, and adds tools for planning and collaboration. GitHub is based on Git, but Git works without GitHub.
Does GitHub store my code on my computer or in the cloud?
Both copies exist. Your computer holds a local repository with the full history, and GitHub holds a hosted copy that Git calls a remote. Changes only move between them when you run commands such as git push, git fetch or git pull.
What is a fork on GitHub?
A fork is a new repository that starts as a copy of another one, called the upstream repository. It has its own settings and permissions but stays connected to the upstream repository. It is a separate repository, unlike a branch, which lives inside one repository.
What does GitHub Actions do?
GitHub Actions is a CI/CD platform that automates build, test and deployment pipelines. You define a workflow in a YAML file stored in the .github/workflows directory of a repository, and repository events such as a push or a new pull request can trigger it.

Sources