Ignoring Files and Undoing Mistakes
Keep secrets and clutter out with .gitignore, and choose the right undo: restore, amend, revert or reset, depending on whether work is shared.
Welcome back!
Step 1 of 14Learn
Two classic first-week mistakes
In his first week on a project, Omar commits a folder called node_modules, 38,000 files of downloaded dependencies, making the repository huge and every status check slow. Later that week he commits a file named .env that contains an API key for the company's payment provider, and pushes it to GitHub.
The first mistake is a nuisance. The second is a security incident. Automated bots scan public repositories for leaked keys, and a pushed key should be treated as compromised within minutes. The fix is to revoke the key with the provider and issue a new one. Deleting the file in a new commit doesn't help, because the key stays in history.
Both mistakes are prevented by one small file: .gitignore. And when mistakes do happen, Git has several ways to undo them, each suited to a different situation. The most important question for choosing one is: has this work already been shared with others? Undoing private, local work is easy and flexible. Undoing shared history needs a gentler tool, because other people's copies depend on it.
Step 2 of 14Learn
Writing a .gitignore file
A .gitignore file in the root of the repository lists patterns. Git won't show matching untracked files in git status or add them with git add .. Commit .gitignore itself so the whole team shares the rules.
# Dependencies and builds
node_modules/
dist/
# Secrets
.env
*.pem
# Logs, except one we want
*.log
!keep.log
# Only the build folder at the root
/buildThe pattern rules:
- A name such as
.envmatches that file in any folder. - A trailing slash (
node_modules/) matches folders only. *matches any characters within a name:*.logignores every log file.**matches across folders:docs/**/*.pdfmatches PDFs anywhere under docs.- A leading slash (
/build) anchors the pattern to the repository root. !re-includes something an earlier pattern excluded.- Lines starting with
#are comments.
You don't need to write one from scratch. GitHub offers templates for most languages when you create a repository, and the github/gitignore collection covers Node, Python, Java and many more.
Step 3 of 14Practice · Diagram
What a typical .gitignore keeps out
Which item would have caused Omar the security problem?
Tap the shape that answers the question, then press Check.
- Project folder
- Commit these
Source code and settings everyone needs.
- Ignore these
Things that are generated, personal or secret.
- src/ and tests/
- .gitignore itself
Share the rules with the whole team.
- package-lock.json
Records exact dependency versions so installs match.
- node_modules/
Downloaded dependencies; anyone can reinstall them.
- .env
Passwords and API keys. Never commit.
- dist/ and *.log
Build output and logs regenerate on every run.
Step 4 of 14Practice · Match the pairs
Match each .gitignore pattern to what it ignores.
Tap an answer, then tap the place where it belongs (or drag it there).
- *.log
- node_modules/
- /build
- !important.log
- .env
Step 5 of 14Note
Step 6 of 14Learn
Undoing local changes: restore, amend and stash
For work that hasn't been committed, or commits that haven't been pushed, undoing is simple.
Discard changes to a file and return it to its last committed state:
git restore chili.md
This throws away your edits for good, since they were never committed, so be sure. (Older tutorials use git checkout -- chili.md, which does the same thing.)
Unstage a file but keep your edits in the working directory:
git restore --staged chili.md
Fix the last commit. Forgot a file or made a typo in the message? Stage the fix and run:
git commit --amend
This replaces the last commit with a new one that includes the staged changes and lets you edit the message. Because it creates a new commit with a new hash, only amend commits you haven't pushed.
Put work aside temporarily. You're mid-change and need to fix something urgent on a clean tree:
git stash saves your uncommitted changes and cleans the working directory. When you're ready, git stash pop brings them back. git stash list shows what's on the shelf.
Step 7 of 14Practice · Diagram
How risky is each undo?
Which command can destroy uncommitted edits across all files?
Tap the shape that answers the question, then press Check.
- git stash
Shelves changes safely;
git stash popbrings them back.- git revert
Adds a new opposite commit. Safe even on shared branches.
- amend (unpushed)
Replaces the last commit. Fine before pushing.
- git restore file
Throws away uncommitted edits to that file for good.
- reset --hard
Moves the branch and wipes uncommitted work. Commits survive in the reflog; uncommitted edits do not.
Step 8 of 14Practice · What would you do?
What would you do?
You've been experimenting in styles.css for an hour and made things worse. Nothing is staged or committed. You want the file exactly as it was in the last commit. What do you run?
Step 9 of 14Learn
Undoing commits: revert and reset
Two commands undo commits, and they differ in one crucial way.
git revert <commit> creates a new commit that applies the opposite of the chosen commit's changes. If commit a1b2c3d added three lines, the revert commit removes them. History is preserved: both the original and the revert are visible. This is safe on shared branches, because nobody's existing history changes. It's the right tool for "the change we pushed yesterday broke checkout; undo it."
git reset <commit> moves your current branch pointer back to an earlier commit, as if later commits never happened on this branch. It has three modes:
--soft: move the branch; keep the undone changes staged.--mixed(default): move the branch; keep the changes in the working directory, unstaged.--hard: move the branch and discard the changes from staging and working directory.
git reset --soft HEAD~1 is a handy way to undo your last local commit and redo it differently. git reset --hard is the most dangerous everyday command, because it deletes uncommitted work.
The golden rule: don't rewrite history that others have pulled. Reset and amend rewrite; revert adds. On shared branches, revert.
Step 10 of 14Compare
revert vs. reset
| Aspect | git revert | git reset |
|---|---|---|
| What it does | Adds a new commit that cancels an old one | Moves the branch pointer to an earlier commit |
| History | Preserved; both commits remain visible | Rewritten; later commits disappear from the branch |
| Safe on pushed, shared branches? | Yes | No, others will have conflicting history |
| Typical use | Undo a bad change already on main | Clean up local commits before sharing |
| Risk | Low | --hard discards uncommitted work |
Step 11 of 14Practice · Sort into groups
Sort these actions by whether they are safe on commits already pushed to a shared branch.
Tap an item, then tap its group (or drag it there).
Step 12 of 14Note
Step 13 of 14Practice · True or false
If you run git reset --hard while you have uncommitted edits, git reflog can bring those edits back.
Step 14 of 14Practice · Choose one
A commit pushed to main yesterday introduced a bug. Teammates have already pulled it. What is the safest way to undo it?
Lesson complete!