Git and GitHub¶
45 min Foundations Needs: a terminal, a GitHub account
Git saves snapshots of your work. GitHub stores those snapshots online so you do not lose them and other people can see them. In this tutorial you set both up, then push your first project to GitHub.
Why this matters¶
You will write a lot of code in the next nine months. Some of it will work and then stop working, and you will want the version from yesterday back. Git gives you that.
There is a second reason. When you apply for a job, your GitHub profile is the first thing a hiring manager opens. Every project you finish here should end up there. Start building that history on day one.
How Git works¶
Four places hold your work. Three of them are on your computer, one is on GitHub. Each command below moves your work from one place to the next.
Keep this picture in your head. Most Git confusion comes from not knowing which of the four boxes your work is sitting in right now.
Step 1: Tell Git who you are¶
Git writes your name on every snapshot you save. Set it once and you never think about it again.
git config --global user.name "Your Full Name"
git config --global user.email "your-email@example.com"
Use the same email address as your GitHub account. If the two do not match, your commits show up on GitHub without your name attached to them.
Nothing prints when this works. Check it like this:
Step 2: Let your computer talk to GitHub¶
GitHub stopped accepting passwords in the terminal. You need a token instead. There are two ways to set this up. The first is easier, so try it first.
The gh tool does the whole login for you and stores the token safely.
Answer the questions like this: GitHub.com, HTTPS, yes authenticate Git with your GitHub credentials, login with a web browser. Copy the one time code it shows you, press Enter, and finish in the browser that opens.
Check it worked:
Use this if gh will not install on your machine.
- On GitHub, open Settings, then Developer settings, then Personal access tokens, then Fine-grained tokens.
- Click Generate new token. Give it a name and an expiry of 90 days.
- Under Repository access, choose All repositories. Under Permissions, set Contents to Read and write.
- Click generate, then copy the token. GitHub shows it once and never again, so paste it somewhere safe right now.
Then tell Git to remember it:
The first time you push, Git asks for a username and password. Type your GitHub username, then paste the token as the password. Git saves it and stops asking.
Do not put your token in a URL
You will find advice online that says to clone like this:
git clone https://username:token@github.com/.... It works, and it is a
bad habit. Git writes that URL into a file inside your project, and your
shell saves it in your command history. One screenshot in a chat channel
and someone else has full access to your account. Use one of the two
methods above instead.
Step 3: Make the repository on GitHub first¶
A repository, or repo, is one project folder that Git is tracking. It is easier to create it on GitHub and copy it down than to create it locally and connect it afterwards.
- On GitHub, click the green New button.
- Name it
my-ml-project. - Leave it public.
- Leave Add a README file unchecked.
- Click Create repository.
You now have an empty repo and GitHub shows you its address.
Step 4: Copy it to your computer¶
Cloning downloads the repo and connects your local folder to the GitHub one.
You should see something like this:
The warning is fine. The repo is empty because you have not put anything in it yet.
Step 5: The four commands you will use every day¶
Make a file, check what changed, save it, send it to GitHub. That is the whole loop, and you will run it hundreds of times.
See what Git noticed. Run this whenever you are unsure what state you are in. It is the most useful command in Git.
Git reports README.md as untracked. It can see the file but is not saving it
yet.
Pick what to save.
Save it. The message says what you changed. Write it for the version of you who reads it in three months.
Send it to GitHub.
Refresh the repo page in your browser. Your file is there.
Write commit messages in the present tense
Say "Add data loader", not "Added data loader". Git's own messages are written that way, so your history reads consistently. Also, if your message needs the word "and", you are probably saving two things at once. Make two commits.
When something goes wrong¶
fatal: not a git repository
You are in the wrong folder. Run pwd to see where you are, then cd into
the project folder you cloned.
Authentication failed when you push
Your token is wrong, expired, or you typed your GitHub password instead of
the token. Generate a new token and run gh auth login again, or delete
the saved credentials with rm ~/.git-credentials and push again.
Updates were rejected because the remote contains work
Someone changed the repo on GitHub after you last downloaded it. Run
git pull, deal with any conflicts, then push again.
Please tell me who you are
You skipped Step 1. Set your name and email, then commit again.
Check yourself¶
Do this now, before moving on. It takes five minutes and proves the whole setup works.
- Add a line to your
README.mddescribing what you plan to build this year. - Run
git statusand read what it tells you. - Stage, commit, and push the change.
- Open the repo on GitHub and find your commit under the commits link.
- Post the repository link in the cohort channel.
If step 3 fails, the problem is almost always authentication. Go back to Step 2.
Quick reference¶
| Command | What it does | When you run it |
|---|---|---|
git status |
Shows what changed and what is staged | Any time you are unsure |
git add file.py |
Stages one file | After editing |
git add . |
Stages everything you changed | When all of it belongs together |
git commit -m "message" |
Saves a snapshot on your machine | After staging |
git push |
Sends your snapshots to GitHub | After committing |
git pull |
Brings down other people's work | Before you start working |
git log --oneline |
Lists your past snapshots | When looking for an old version |
Next¶
You have version control working. Next you need somewhere to run code and see results straight away.
Want to go deeper on Git later: the Pro Git book is free and thorough, and Learn Git Branching lets you practise branching visually.