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
- Git vs GitHub: Git is the tool, GitHub hosts a shared copy called a remote.
- SSH keys: a key pair proves who is pushing; the private key never leaves the computer.
- Push, pull and fetch: a rejected push means pull first; force-push only your own branch, with a lease.
- Cloning and forking: clone what you can push to; fork what you can't, and sync it from
upstream. - Pull requests: a branch, a description, a review, then merge.
- Merge strategies: merge commit keeps everything, squash makes one commit, rebase keeps a straight line.
- Issues and Projects: one issue per piece of work, closed by
Fixes #12. - Branch protection: GitHub enforces the rules; CODEOWNERS picks reviewers.
- Releases and tags: MAJOR.MINOR.PATCH, annotated tags, and release notes.
Key terms
| Term | Meaning |
|---|---|
| Remote | Another copy of the repository, usually on GitHub |
origin | The usual name of the remote a repository was cloned from |
upstream | The usual name of the original project, for a fork |
origin/main | The local bookmark of where main was on the remote at the last fetch |
| Tracking branch | A local branch linked to a remote one, so plain push and pull work |
| Ahead / behind | Commits you have that the remote doesn't / the other way round |
| SSH key pair | A public key (given to GitHub) and a private key (kept secret) |
| Personal access token | A password-like code for Git over HTTPS |
| Fetch | Download new commits without changing local branches |
| Pull | Fetch, then merge or rebase |
| Force-push | Replacing the remote's history with yours |
| Clone | A full copy of a repository on your computer |
| Fork | A copy of someone else's repository under your GitHub account |
| Pull request | A request, with a review page, to merge one branch into another |
| Squash merge | All of a pull request's commits combined into one |
| Issue | A numbered page for one bug, feature or question |
| Ruleset | GitHub rules a branch must follow, such as requiring reviews |
| CODEOWNERS | A file naming who reviews which parts of the code |
| Tag | A permanent name for one commit, usually a version |
| Semantic versioning | MAJOR.MINOR.PATCH: breaking, feature, fix |
| Release | GitHub's page for a tag, with notes and downloads |
Essential commands
| Command | What it does |
|---|---|
ssh -T git@github.com | Tests the SSH connection |
git clone url | Copies a repository to the computer |
git remote -v | Lists remotes |
git remote add upstream url | Adds the original project to a fork |
git push -u origin branch | Pushes a new branch and tracks it |
git fetch | Downloads without changing anything |
git pull --rebase | Fetches and replays local commits on top |
git log main..origin/main | Shows what the remote has that you don't |
git push --force-with-lease | Force-pushes only if nobody else pushed |
git fetch --prune | Removes bookmarks of deleted branches |
git push origin --delete branch | Deletes a branch on the remote |
gh pr create | Opens a pull request |
gh pr checkout 42 | Tries pull request 42 locally |
git tag -a v1.2.0 -m "…" | Tags a version |
git push origin v1.2.0 | Pushes a tag |
Rules to remember
- Never share the private key. Only the
.pubfile goes to GitHub. - A rejected push protects someone's work. Pull, then push again.
- Fetch is always safe. Look with
git log main..origin/mainbefore merging. - Never force-push a shared branch, and use
--force-with-leaseon 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.