Skip to content
Author: ytianle

Ignore Tracked Files⚓︎

We are going to use git rm -r --cached <file path> here.

Ignore file back⚓︎

When the files are tracked, directly ignoring them in the git ignore doesn't work. The correct steps are:

  1. Remove index

    terminal
    git rm --cached <path/to/file>
    git commit -m "Remove tracked file" -- notice there is no add will be needed.
    

  2. Update gitignore

    .gitignore
    <path/to/file>/
    **/<path/to/file>
    

Refresh index⚓︎

You can also use this command to refresh the tracking of the file.

terminal
git rm --cached <path/to/file>
git add .
git commit -m "Refresh the index of the file."
Will this affect the time stamp of the tracked file?

Someone might be curious: will this tracking refreshment affect the timestamp of the file?

The answer is no. Below is a table that can be helpful to explain what it does.

Aspect Does git rm --cached care? Explanation
Git index (staging area) ✅ Yes Primary target: paths are removed from the index
File paths ✅ Yes Tells Git these paths should no longer be tracked
Current commit diff ✅ Yes Appears as deleted in the next commit
Permanent removal from repo ⚠️ Depends on usage Permanent only if not re-added with git add
Working tree files (disk) ❌ No Files remain on disk
File content ❌ No Contents are not modified
Existing Git commits ❌ No Past commits and history are untouched
Rename / move history tracking ❌ No History can still be associated via content similarity
MkDocs git-revision-date plugins ❌ (Indirect) Git-based timestamps usually remain intact
OS file timestamps (mtime/ctime) ❌ No Git ignores filesystem timestamps
Remote repository ❌ No No effect until commit and push

Restore the change⚓︎

  • Case 1: Already executed git rm -r --cached docs but have not committed yet -> git restore --staged docs.
  • Case 2: Have committed, but have not pushed -> git reset --soft HEAD~1.