Git & GitHub¶
In one sentence
Git is a program on your own computer that keeps save points of your code. GitHub is a website that keeps a copy of those save points online, so your code is backed up and other people can work on it with you.
This page is the lesson of 17 October. Everything below is what we typed in class, in the same order. Keep it open the next time you set up a new folder.
1. Git or GitHub?¶
| Git | GitHub | |
|---|---|---|
| what it is | a program (git in the terminal) |
a website |
| where it lives | on your computer | on the internet |
| what it does | keeps save points (commits) of a folder | keeps a copy of your commits online, shows them on a web page, helps people work together |
GitHub is not the only website for this; GitLab and Gitee do the same job. Git works fine on its own without any of them.
2. Once per computer¶
Install Git first. Then tell Git who you are. Every commit records this name and email:
git config --global user.name "Liam"
git config --global user.email "the-email-of-your-github-account"
Use the same email as your GitHub account. That is how GitHub knows the commits are yours. --global means "for every folder on this computer", so you only do this once.
SSH key¶
GitHub needs to know that a push really comes from you. We made a pair of keys for that:
ssh-keygen -t ed25519 -C "the-email-of-your-github-account"
Press Enter for every question. This creates two files in the .ssh folder in your home folder:
| File | Name | Who may see it |
|---|---|---|
id_ed25519 |
private key | only you. Never send it to anyone or upload it anywhere, not even to Google Drive. Anyone who has it can push to your GitHub account. |
id_ed25519.pub |
public key | anyone. This is the one you give to GitHub. |
Print the public key and copy the whole line:
cat ~/.ssh/id_ed25519.pub
On GitHub: Settings → SSH and GPG keys → New SSH key, give it a title (for example the name of your laptop) and paste the line.
One key per computer is enough. Once GitHub has it, you can push to every repository of your account from this computer.
3. Once per project folder¶
Go into the folder in the terminal (the terminal inside CLion is already in the project folder), then:
git init
This turns the folder into a Git repository. It creates a hidden folder called .git, which is where Git keeps all the save points. You only run git init once per folder.
.gitignore¶
git status showed many files we do not want to save: the settings of our tools, not our code. Create a file called .gitignore in the folder and list them, one per line. Ours was:
.vscode
.idea
.autocp
.clang-format
A name starting with a dot is a hidden file. Files listed in .gitignore turn orange in CLion and Git never adds them.
First commits¶
git add .gitignore
git commit -m "Initialize repo and add .gitignore"
git add .
git commit -m "Add all code"
The Git panel in CLion now shows two commits: the first has one file, the second has everything else.
Connect to GitHub¶
On GitHub click New repository, give it a name, and leave README, .gitignore and license unticked: our folder already has its own history, so the GitHub repository must start empty.
The page that opens shows the commands for an existing repository. Copy them into the terminal. They look like this (with your own user name and repository name):
git remote add origin git@github.com:USERNAME/REPO.git
git branch -M main
git push -u origin main
Refresh the GitHub page and your code is there.
origin is just a nickname for the GitHub address. If the address is wrong, change it with git remote set-url origin <address>. Use the address that starts with git@github.com:, because that is the one that uses your SSH key.
4. Every time you finish something¶
Nothing goes to GitHub by itself. Every time, three steps:
git add .
git commit -m "AC: Two Buttons"
git push
| Command | What it does |
|---|---|
git status |
shows what has changed and what is ready to commit. Run it whenever you are not sure what is going on. |
git add . |
puts every changed file into the next save point (except the ones in .gitignore) |
git commit -m "..." |
makes the save point, with a short message saying what you did |
git push |
sends your new commits to GitHub |
git pull |
receives commits from GitHub that are not on your computer yet |
The local branch main and the GitHub branch main do not stay in sync on their own: push and pull are how you sync them.
5. Branches¶
A branch is a separate copy of the code where you can try something without touching main. What we did in class:
- On GitHub, create a branch called
devfrommain. - On your computer,
git pullso that your computer knows aboutdev. -
Switch to it and check:
git checkout dev git statusgit statusnow saysOn branch dev. -
Create a new file, then
git add .,git commit -m "...",git push. The file is ondevonly;maindoes not have it. - On GitHub, open a pull request from
devintomainand click Merge. - Back on your computer,
git checkout mainandgit pull. Nowmainhas the file too, and the commit graph in CLion showsdevjoining back intomain. devis not needed any more, so it can be deleted on GitHub.
Why bother? In a team, everyone works on their own branch (one person on the login page, another on the register page). A pull request lets the others look at and test the change before it goes into main, so main always works. Working alone, main is enough for now.
Credits
Written from the lesson of 17 October. The commands are the ones typed in class.