Ignoring Files and Undoing Changes

Keep temporary and private files out of commits. When you make a mistake, choose the command based on whether the change is staged or already committed.

On this page

Write a .gitignore file

Save these patterns in .gitignore at the root of project-notes. Run git add .gitignore and commit it so the whole team can use the same rules. The rules apply to untracked files. If a file is already tracked, adding a pattern does not remove it from an existing commit. Keep passwords and API keys out of the repository.

.env
*.log
node_modules/
.venv/

Unstage a change and keep the file

In the practice repository, make a small edit to README.md and stage it. git restore --staged removes that change from the next commit while preserving the edit in your working tree. git diff then shows the unstaged edit. Stage the file again when the edit is ready.

git add README.md
git restore --staged README.md
git diff -- README.md

Undo a recorded change with a new commit

For a commit already shared with others, git revert records a new commit that reverses its changes. With a clean working tree, choose an ordinary, non-merge commit from git log and replace abc1234 with its actual ID. If later changes overlap, Git may ask you to resolve a conflict.

git log --oneline
git revert abc1234

Official references