Git and GitHub · Part 3 · GitHub and remote repositories

Part 3 summary: GitHub and remote repositories

The big idea

GitHub keeps a shared copy of the repository. Everyone pushes their branches to it, and nothing reaches main without a pull request that others can review and checks can test. Rules on GitHub make that the only way in, and tags mark the versions that ship.

Each lesson in one line

  1. Git vs GitHub: Git is the tool, GitHub hosts a shared copy called a remote.
  2. SSH keys: a key pair proves who is pushing; the private key never leaves the computer.
  3. Push, pull and fetch: a rejected push means pull first; force-push only your own branch, with a lease.
  4. Cloning and forking: clone what you can push to; fork what you can't, and sync it from upstream.
  5. Pull requests: a branch, a description, a review, then merge.
  6. Merge strategies: merge commit keeps everything, squash makes one commit, rebase keeps a straight line.
  7. Issues and Projects: one issue per piece of work, closed by Fixes #12.
  8. Branch protection: GitHub enforces the rules; CODEOWNERS picks reviewers.
  9. Releases and tags: MAJOR.MINOR.PATCH, annotated tags, and release notes.

Key terms

TermMeaning
RemoteAnother copy of the repository, usually on GitHub
originThe usual name of the remote a repository was cloned from
upstreamThe usual name of the original project, for a fork
origin/mainThe local bookmark of where main was on the remote at the last fetch
Tracking branchA local branch linked to a remote one, so plain push and pull work
Ahead / behindCommits you have that the remote doesn't / the other way round
SSH key pairA public key (given to GitHub) and a private key (kept secret)
Personal access tokenA password-like code for Git over HTTPS
FetchDownload new commits without changing local branches
PullFetch, then merge or rebase
Force-pushReplacing the remote's history with yours
CloneA full copy of a repository on your computer
ForkA copy of someone else's repository under your GitHub account
Pull requestA request, with a review page, to merge one branch into another
Squash mergeAll of a pull request's commits combined into one
IssueA numbered page for one bug, feature or question
RulesetGitHub rules a branch must follow, such as requiring reviews
CODEOWNERSA file naming who reviews which parts of the code
TagA permanent name for one commit, usually a version
Semantic versioningMAJOR.MINOR.PATCH: breaking, feature, fix
ReleaseGitHub's page for a tag, with notes and downloads

Essential commands

CommandWhat it does
ssh -T git@github.comTests the SSH connection
git clone urlCopies a repository to the computer
git remote -vLists remotes
git remote add upstream urlAdds the original project to a fork
git push -u origin branchPushes a new branch and tracks it
git fetchDownloads without changing anything
git pull --rebaseFetches and replays local commits on top
git log main..origin/mainShows what the remote has that you don't
git push --force-with-leaseForce-pushes only if nobody else pushed
git fetch --pruneRemoves bookmarks of deleted branches
git push origin --delete branchDeletes a branch on the remote
gh pr createOpens a pull request
gh pr checkout 42Tries pull request 42 locally
git tag -a v1.2.0 -m "…"Tags a version
git push origin v1.2.0Pushes a tag

Rules to remember

  • Never share the private key. Only the .pub file goes to GitHub.
  • A rejected push protects someone's work. Pull, then push again.
  • Fetch is always safe. Look with git log main..origin/main before merging.
  • Never force-push a shared branch, and use --force-with-lease on your own.
  • Everything reaches main through a pull request, small, about one thing, with a clear description.
  • Let GitHub enforce the rules with a ruleset on main.
  • Never move a published tag. Release a new version instead.