.gitignore and pull requests · Git and GitHub
How do teams keep a repository clean and safe?
Course contentsWhy you need it
A repository is shared and permanent. Secrets, build output and log files don't belong in it: secrets leak, and junk files cause pointless conflicts. Teams agree on a few habits: a .gitignore, small branches, clear messages and reviewed pull requests.
The intuition
.gitignore lists patterns Git should pretend not to see:
.env
*.log
build/Ignored files don't show in git status and git add . skips them. (A file that is already committed stays tracked; ignoring it later doesn't remove it from history.)
A typical change on a team:
git switch -c fix-cart-total- small commits with clear messages
git push -u origin fix-cart-total- open a pull request on GitHub; a teammate reviews it; CI runs the tests
- merge, then delete the branch
Definition
.gitignore
A file of patterns for files Git should not track: secrets, logs, build output, dependencies such as node_modules/.
Pull request
A request on GitHub (or a similar site) to merge a branch, where teammates review the changes and automated checks run before merging.
Examples
Ignore what doesn't belong
Run it in the lessonThree files that must never be committed. Write .gitignore first, then add everything.
git status
echo ".env" > .gitignore
echo "*.log" >> .gitignore
echo "build/" >> .gitignore
git status
git add .
git commit -m "add gitignore"- L1Before: .env, build/app.bin and debug.log all show as untracked.
- L5After: only .gitignore itself is new.
- L6
git add .skips the ignored files.
Tag a release
Run it in the lessonA tag is a fixed name for one commit, used for releases.
git tag v1.0
git tag
git log --oneline- L1v1.0 now always means this commit.
- L3The tag shows next to the commit.
Common mistakes
Ignoring after committing
Adding .env to .gitignore after it was committed doesn't remove it from history. Change the secret, and remove the file from tracking with git rm --cached .env.
Huge pull requests
A 2,000-line pull request gets skimmed, not reviewed. Several small ones get merged faster and catch more bugs.
Quick quiz
1. Which line in .gitignore ignores every file ending in .log, in any folder?
Show the answer
Answer: A
A pattern without a slash matches the file name anywhere.
2. What does the last command print?
echo "*.log" > .gitignore
git add .
git statusShow the answer
Answer: A
Only *.log is ignored, so the secret .env and build/ were staged. That's the mistake .gitignore is meant to prevent.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWhat gets committed?
After these commands, which files are in the new commit?
- ModifyKeep the secret out
This commits everything, including the
.envsecret, the log and the build folder. Add a.gitignorebeforegit add .so only.gitignoreis committed. - WriteReady for a pull request
Do the first half of the team routine: create the branch
fix-total, changeapp.pyso it containsprint('total fixed')(one line), commit it with the messagefix total, and push the branch to origin with tracking (-u). Don't commit.env,debug.logorbuild/.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| .gitignore | .gitignore 忽略文件 | A list of file patterns Git should not track. |
| Pull request | 拉取请求 | A reviewed request to merge one branch into another. |
| Tag | 标签 | A fixed name for one commit, often a release like v1.0. |