Everyone Can Learn Program
中文 Start learning
Core · GitLesson 6 of 6Free to read

.gitignore and pull requests · Git and GitHub

How do teams keep a repository clean and safe?

Course contents

Why 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:

  1. git switch -c fix-cart-total
  2. small commits with clear messages
  3. git push -u origin fix-cart-total
  4. open a pull request on GitHub; a teammate reviews it; CI runs the tests
  5. 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 lesson

Three 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"
Step by step
  1. L1Before: .env, build/app.bin and debug.log all show as untracked.
  2. L5After: only .gitignore itself is new.
  3. L6git add . skips the ignored files.

A tag is a fixed name for one commit, used for releases.

git tag v1.0
git tag
git log --oneline
Step by step
  1. L1v1.0 now always means this commit.
  2. 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?

  1. A*.log
  2. B/log
  3. C.log/
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 status
  1. A
    On branch main
    Changes to be committed:
      new file:   .env
      new file:   .gitignore
      new file:   build/app.bin
  2. B
    On branch main
    Changes to be committed:
      new file:   .gitignore
  3. C
    On branch main
    Changes to be committed:
      new file:   .env
      new file:   .gitignore
      new file:   build/app.bin
      new file:   debug.log
Show 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.

  1. PredictWhat gets committed?

    After these commands, which files are in the new commit?

  2. ModifyKeep the secret out

    This commits everything, including the .env secret, the log and the build folder. Add a .gitignore before git add . so only .gitignore is committed.

  3. WriteReady for a pull request

    Do the first half of the team routine: create the branch fix-total, change app.py so it contains print('total fixed') (one line), commit it with the message fix total, and push the branch to origin with tracking (-u). Don't commit .env, debug.log or build/.

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

TermChineseDefinition
.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.