Lesson 14 of 145 min
Git and GitHub
Git keeps every snapshot you commit of a project's folder, and GitHub keeps a copy of them where others can read it, so a file with your key must never be committed.
After this lesson you can save a project's changes with Git, put it on GitHub, and copy someone else's repository.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
Tell Git who you are, once
Every commit carries a name and an email. Git asks for them before your first commit on a new computer. Set them once and every project uses them. On GitHub, use the email of your account, or the private address GitHub gives you in its email settings.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global user.nameYour Name
Signing in, and GitHub's own tool
GitHub doesn't take your password in the terminal. On Windows, Git comes with its own sign-in helper, which opens a browser window the first time you push. On a Mac, GitHub's tool gh does the same. With it, one line makes the repository on GitHub and pushes your commits.
gh auth login
gh repo create hub-stats --public --source=. --pushCaught before the commit
git status lists what the commit will take. If you see .env there, take it out before you commit: git rm --cached takes it off the list and leaves the file where it is. Then write the .gitignore, so the next git add . leaves it out too.
git rm --cached .env
echo ".env" > .gitignore
git add .gitignore
git status --shortrm '.env' A .gitignore A hub.py A main.py
A .gitignore for Python
GitHub keeps a ready .gitignore for Python projects. It lists .env, .venv, __pycache__/ and more: pick Python under "Add .gitignore" when you make the repository. The environment keeps itself out anyway. Since Python 3.13, python -m venv .venv puts a .gitignore inside .venv that says to ignore everything there.
python -m venv .venv
cat .venv/.gitignore# Created by venv; see https://docs.python.org/3/library/venv.html *
See what changed, and go back
git diff shows each line you changed since the last commit: a minus for the old line, a plus for the new one. git restore puts the file back as the last commit had it. git log --oneline lists the commits, newest first, each with its id.
git diff@@ -1,3 +1,3 @@
from hub import downloads
-print(downloads("gpt2"))
+print(downloads("distilgpt2"))git restore main.py
git log --onelineeed23f1 First version
If a key was pushed
Make a new key and turn off the old one, at once. For Hugging Face, both are on its Access Tokens page. A new commit doesn't help, and neither does rewriting the history: anyone may have copied the key already. A key that is turned off gets status 401, as a made-up one does.
curl -s -o /dev/null -w "%{http_code}\n" \
-H "Authorization: Bearer hf_abc123" \
https://huggingface.co/api/whoami-v2401
Run on Git 2.40.0, Python 3.13.3 and GitHub CLI 2.92.0, on a Mac. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
You commit .env, which holds your key, and push to a public repository. Then you delete .env, commit and push again. Is the key safe now?
2 of 2
Your project is already on GitHub. You change main.py, then run git add main.py and git commit -m "Print downloads". Does GitHub's copy have the change?
The ideas in this lesson
- Commits
- Git keeps snapshots of a folder. git add takes the files, and git commit saves them with a message.
- GitHub
- GitHub keeps a repository online. git push sends your commits there, and git clone copies one to your computer.
- Keep the key out
- A .gitignore lists files Git never saves. With .env in it from the start, your key never reaches GitHub.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
Who can read it?
Here's a question. You put your project on GitHub, a website for code. Now you won't lose it, even if your laptop breaks. One small file in the project holds your key.
It's the key for an AI service. Who can read that key now?
Your guess
Only you, because it's your account? Or someone else too? Make your guess.
The key's file
Here's a small project, hub-stats. The file hub.py asks the Hugging Face Hub about a model, and main.py prints its downloads.
Now hub.py also sends your Hugging Face key with each request, as the Hub asks. A key is a password for programs, and it stays out of the code.
So it lives in a file named dot env. The code reads that file when it starts.
Snapshots
How do you keep every version of a project? When you save a file, the old version is gone. Git keeps a snapshot of the whole folder each time you ask. And every snapshot stays.
In the terminal, in the project's folder, the first command is git init. Git starts keeping this folder's history, in a hidden folder called dot git. The rest of that line gives the history a name, main. GitHub uses the same name. The folder is now a repository.
Add, then status
How do you take a snapshot?
First, git add takes the files for it. You name the files, or type a dot for every file in the folder. Then git status lists what it took: dot env, hub.py and main.py.
A commit
Then git commit saves them, with a short message: First version. That's one commit, with its own id.
So is your project on GitHub now?
No. A commit stays on your computer.
GitHub
A repository is a folder whose history Git keeps. GitHub is a website that keeps a copy of it online.
Your copy can be public, for anyone to read. Or private, for the people you choose. With a free account, you make an empty repository there.
Then GitHub shows you a line to type in the terminal. It tells Git where your copy on GitHub is.
Push
Then git push sends your commits there. The first time, it takes a few more words, from GitHub's page, and GitHub asks you to sign in. After that, just git push. Now GitHub's copy holds First version, with its files. If your laptop breaks, your project is still there.
Clone
On a new laptop, or for anyone's project, git clone and the address make a new folder. It holds every file and every commit. Here's the clone. Look at its files. Is something there that should not be?
Anyone
A commit saves the files you added, and a push sends them to GitHub. So, back to the question.
Who can read your key?... If the repository is public, anyone. And anyone who cloned it.
git add took every file in the folder, the key's file too. git status listed it, and nothing on your computer stopped it.
This happens a lot. In 2025, millions of keys and passwords were sent to public repositories on GitHub. GitHub itself stops some kinds of key on the way, but not all.
Delete it later?
So you delete dot env, then add, commit and push again. Is the key safe now?
No. The new commit has no dot env. But First version is still there, on GitHub, just as it was. Git keeps every commit. That's what it's for. So once a key is pushed, you can't take it back. You replace it: on the service's website, make a new key, and turn off the old one.
A .gitignore
So start again, the right way. Before the first commit, make a file named dot gitignore, with one line: dot env. A dot gitignore lists the files Git never saves. Now git add leaves the key's file out, even with a dot. git status lists dot gitignore, hub.py and main.py.
And GitHub's copy gets no key.
A clone has no dot env, so on a new computer you make it again.
What to keep
So: add and commit take a snapshot of your folder. Push sends it to GitHub, and clone copies it to another computer.
And a dot gitignore file keeps your key's file on your own computer, from the first commit.
The lab
That's the end of this unit. In its lab, you use these four lessons on your own computer. You set up a project, run it, and keep it in Git, with the key kept out.
Try it
Git keeps every commit, so keep your key out from the start. Now try it on this lesson's page, and take the quiz.
Keep the key off GitHub, with no gitignore file.
You use this lesson in Lab 5: On your own computer, which comes next.
Facts last checked on .
