Keep the repository clean
- Lesson 6
- 30 min
- Module 2: Work with branches and remotes
Lesson 6 of 15 · Git and Team Development EssentialsKeep the repository clean
1. Understand a repository
2. Work with branches and remotes
3. Collaborate through reviews
4. Resolve and recover
5. Release and hotfix practice
What you will be able to do
Keep passwords, keys and large generated files out of your repository. A file that you commit stays in the history, and everyone who clones the repository gets a copy.
Before you startWhat to know and to have ready
What you need to know
- How
git pushandgit pullwork with the upstream of a branch, from Synchronize deliberately: Your copy of the remote. - How to stage and commit a change, from Track a first change: Three places for your changes.
What you need on your computer
-
The repository
~/projects/ticket-notes, on the branchmain.git statusshowsworking tree clean. If you are on a different branch, rungit switch main. -
The remote
origin, which is the folder~/projects/team-remote.git. You made it in Synchronize deliberately: Make a practice remote. You pushedmainto it in Synchronize deliberately: Guided practice: be both people. The guided practice in this lesson pushes to it again. -
If
git remote -vshows nothing, do Make a practice remote again. Then rungit push -u origin main. -
If the repository is missing or broken, a script can make the start state of this lesson again. In
~/projects, run:Terminal window git clone https://github.com/Nextia-Intelligence/nextia-learning-labs.gitsh nextia-learning-labs/C14/setup.sh m02-l03 --resetRun the
git clonecommand only once. If you already have thenextia-learning-labsfolder, rungit -C nextia-learning-labs pullinstead. Pull-request sequence: Set up the practice lab explains the script in full. The script makesticket-notes,team-remote.gitandsam-notesin~/projects/c14-lab, as at the end of Synchronize deliberately: Guided practice: be both people. Sam’s commitAdd ticket 102has a different hash from the lesson (1d45995), because in the lesson you made that commit yourself. This is expected. Then use~/projects/c14-labin place of~/projectsin this lesson.--resetdeletes an olderc14-labfolder first, but your~/projects/ticket-notesdoes not change.
History keeps everything
Section titled “History keeps everything”Git keeps every version of every file that you commit. If you delete a file in a new commit, the old commits still contain it. Amira committed a settings file, then removed it in the next commit. One command still shows the old contents:
git show c9a1952:settings.localDEBUG=truec9a1952:settings.local means “the file settings.local as it was in commit c9a1952”. When you push, the remote gets the full history, and so does each person who clones it.
So it is much easier to keep a file out of Git than to remove it later. Three kinds of files must stay out:
| Kind of file | Examples | Why it must stay out |
|---|---|---|
| Secrets | .env, API keys, passwords, private keys |
Anyone who can read the repository can use them. |
| Files that tools make | exports/, *.log, .venv/, node_modules/, __pycache__/ |
You can make them again. They change often and make diffs hard to read. |
| Large files | datasets, trained models, .zip archives, videos |
Every clone downloads every version. GitHub refuses files larger than 100 MB. |
.gitignore: tell Git what to leave out
Section titled “.gitignore: tell Git what to leave out”A .gitignore file is a list of patterns, one on each line. Git does not show matching untracked files in git status, and git add does not stage them. You commit .gitignore itself, so the whole team uses the same rules.
| Pattern | Matches |
|---|---|
.env |
A file or folder called .env, in any folder. |
exports/ |
A folder called exports, and everything in it. The / at the end means “only a folder”. |
*.log |
Every file whose name ends with .log. * means “any text”. |
# text |
A comment. Git ignores the line. |
Worked example: ignore a key, an export and a log
Section titled “Worked example: ignore a key, an export and a log”Amira’s ticket tool needs a key, so she keeps it in a file called .env. The tool also makes exports/tickets-2026.zip and sync.log. git status --short shows all three as untracked:
?? .env?? exports/?? sync.logShe makes .gitignore in the top folder of the repository:
# Secrets: never commit them.env
# Files that tools makeexports/*.logNow git status --short shows only the new .gitignore:
?? .gitignoreTo check which rule matches a file, she uses git check-ignore -v. The output shows the file, the line number and the pattern:
git check-ignore -v .env exports/tickets-2026.zip sync.log.gitignore:2:.env .env.gitignore:5:exports/ exports/tickets-2026.zip.gitignore:6:*.log sync.logIf someone tries to stage an ignored file, Git refuses:
git add .envThe following paths are ignored by one of your .gitignore files:.envhint: Use -f if you really want to add them.Do not use -f for a secret. Amira commits .gitignore:
git add .gitignoregit commit -m "Ignore secrets, exports and logs"Worked example: a file that Git already tracks
Section titled “Worked example: a file that Git already tracks”.gitignore affects only untracked files. If you committed a file before you added its rule, Git continues to track it.
Amira committed settings.local, then added settings.local to .gitignore and changed the file. Git still shows the change:
M .gitignore M settings.localTo stop tracking the file but keep it on her computer, she uses git rm --cached. --cached means “remove it from the staging area only”. Without --cached, git rm also deletes the file from the folder.
git rm --cached settings.localgit status --shortrm 'settings.local' M .gitignoreD settings.localD in the first column means that the next commit removes the file from the repository. She stages .gitignore too, and commits both:
git add .gitignoregit commit -m "Stop tracking settings.local"settings.local is still in her folder, and Git ignores it now. But the old commit c9a1952 still contains it. This is acceptable for a debug setting. For a secret, it is not: see the common mistake below.
Small commits
Section titled “Small commits”A clean repository also has clear commits. A reviewer reads a commit to understand one change. If a commit mixes three changes, the reviewer must separate them in their head.
- One purpose for each commit. “Add priority levels” is one purpose. “Add priority levels, fix typo, and update contacts” is three commits.
- Check before you commit. Run
git diff --staged, and read every line. Look for files that you did not mean to add. - Write the message as an instruction. “Add ticket 104”, not “Added stuff”. Say why in a second paragraph when the reason is not clear from the change.
Guided practice: ignore your own files
Section titled “Guided practice: ignore your own files”-
In
~/projects/ticket-notes, make three files that Git must not track. Use your editor, or run these commands:Terminal window echo "TICKETS_API_KEY=example-not-a-real-key" > .envmkdir exportsecho "export" > exports/tickets.csvecho "sync started" > sync.logecho "text" > filewrites the text into a new file. -
Run
git status --short. All three show??. -
Make
.gitignorewith the five lines from the worked example. Save it. -
Run
git status --short. Only?? .gitignoreshows. -
Check the rules:
Terminal window git check-ignore -v .env exports/tickets.csv sync.logEach file shows the line of
.gitignorethat matches it. -
Commit
.gitignore, and push it so your team gets the same rules:Terminal window git add .gitignoregit commit -m "Ignore secrets, exports and logs"git push
Your turn: rules for a Python project
Section titled “Your turn: rules for a Python project”Your team adds Python to the ticket tool. Now the folder has these new items:
.venv/: a Python environment, 400 MB.data/raw/tickets-full.csv: a 2 GB export of real tickets.data/sample.csv: 20 made-up tickets for tests. The team wants this file in Git.__pycache__/: files that Python makes.
Add rules to .gitignore so that Git ignores everything except data/sample.csv. Make the files and folders, then prove your rules with git status --short and git check-ignore -v.
Show a possible answer
# Python.venv/__pycache__/
# Large or private datadata/raw/git status --short then shows M .gitignore and ?? data/. data/ shows because data/sample.csv is untracked and not ignored. git check-ignore -v data/sample.csv prints nothing, which means that no rule ignores it. data/raw/ has a second reason to stay out: real tickets can contain personal data.
Common mistake
I committed a secret
Symptom: git log --stat or a review shows .env, a key or a password in a commit.
Cause: the file was staged before a .gitignore rule existed, or someone used git add -f.
Fix: act in this order.
- Treat the secret as public. If you pushed the commit, other people and services can have a copy. Go to the service that issued the key, and revoke it or make a new one. Do this first. It is the only step that stops misuse.
- Stop tracking the file with
git rm --cached .env, add.envto.gitignore, and commit. - Tell your team what happened and which key you replaced.
The old commit still contains the secret. Removing it from history is possible, but it rewrites shared history, and every clone must be replaced. It also does not help if someone already copied the key. That is why step 1 matters most.
Check your understanding
Amira commits config.json, which contains a password. Then she adds config.json to .gitignore and commits again. Is the password now safe?
Show the answer
No, for two reasons. First, .gitignore does not affect a file that Git already tracks, so Git still tracks config.json. She also needs git rm --cached config.json. Second, the first commit still contains the password. If she pushed it, she must change the password.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Practical check: prove that .env is ignored in your repository, and show which rule does it.
Show the answer
Run git check-ignore -v .env. The output shows .gitignore, the line number and the pattern, for example .gitignore:2:.env .env. If the output is empty, no rule matches the file. Also run git status --short: .env must not appear.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- Git keeps every committed version. Deleting a file later does not remove it from history.
.gitignorekeeps untracked files out ofgit statusandgit add. Commit it so the whole team uses it.- Use
git check-ignore -vto see which rule ignores a file. git rm --cachedstops tracking a file and keeps it in your folder.- If you commit a secret, replace it first. Then remove the file from tracking.
- Make each commit one clear change, and read
git diff --stagedbefore you commit.
This is the end of Module 2. In Module 3, you put these skills together in a pull request: your team reviews your branch before it goes into main.
Next: Pull-request sequence