Git · Lesson 6 of 6
Undoing changes & .gitignore
restore, revert, reset, amend and keeping files out of Git.
Different mistakes need different tools:
git restore file— throw away uncommitted edits to a file.git restore --staged file— unstage, keeping your edits.git commit --amend— fix the last commit's message or add a forgotten file (only before pushing).git revert <commit>— make a new commit that undoes an old one. Safe for shared branches.git reset --soft HEAD~1— undo the last commit but keep its changes staged (local only).
Avoid git reset --hard and force-pushing on shared branches — they rewrite history others depend on.
.gitignore
A .gitignore file lists things Git should never track: node_modules/, .venv/, build output, logs and especially secrets such as .env files.
Example in the terminal
Run these on your own computer$ git status
modified: app.js
$ git restore app.js # discard the edit
$ git log --oneline
a91e0c2 Remove salary filter by mistake
8b41d07 Add salary range filter
$ git revert a91e0c2
[main 5d7f310] Revert "Remove salary filter by mistake"
$ cat .gitignore
node_modules/
.venv/
.env
*.log
Lines starting with $ are commands you type; the rest is what Git prints. Names and URLs are examples.
Key points
- restore = discard local edits.
- revert = safe undo on shared history.
- .gitignore keeps secrets and junk out of the repo.
Quick check
Which command safely undoes a commit that's already pushed to a shared branch?
revert adds a new commit that reverses the old one, without rewriting shared history.
Practice task
Add a .gitignore that ignores *.log and .env, create both files and confirm git status doesn't list them.
Interview corner
git reset vs git revert?
reset moves the branch pointer back (rewriting history) — fine for local work. revert creates a new commit that undoes changes — safe for shared branches.
You committed a password. What do you do?
Rotate the secret immediately (assume it's leaked), remove it from the code, add the file to .gitignore, and if needed purge it from history with a history-rewriting tool — then coordinate with the team.
Further reading
Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.
Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.