日本語

How to Stop Git Tracking Files Committed by Mistake (git rm –cached)

​​Git​command line

If you committed .env, node_modules/, or log files by mistake, adding them to .gitignore is not enough. Run git rm --cached to remove them from Git's tracking, then commit that change. The local files stay on disk, and later commits no longer include them. These are the steps to take when you want to stop committing a config file with passwords or API keys, or build output. I checked the output before and after each operation in a test environment.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and set the commit dates with the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. The contents of .env are dummy values. For the shared repository, I used a bare repository created in the same environment. I did not test with hosting services such as GitHub, or on Windows or macOS.

Example repository: I committed .env, node_modules, and a log file

The starting state is a commit made with git add . that included app.js, .env, debug.log, and node_modules/left-pad/index.js. git ls-files lists the files that Git is tracking.

console
$ git add . && git commit -q -m "最初のコミット"

$ git ls-files
.env
app.js
debug.log
node_modules/left-pad/index.js

Adding files to .gitignore alone does not stop tracking

First, I wrote the three kinds of files I wanted to exclude in .gitignore.

console
$ cat .gitignore
.env
node_modules/
*.log

$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore

nothing added to commit but untracked files present (use "git add" to track)

In this state, changing .env and debug.log makes them appear as modified in git status. Running git add -n . (which does not actually add anything, but shows which files would be added) confirms that .env and debug.log would also be staged.

console
$ printf "API_KEY=dummy-key-2\n" > .env && printf "more\n" >> debug.log

$ git status --short
 M .env
 M debug.log
?? .gitignore

$ git add -n .
add '.env'
add 'debug.log'
add '.gitignore'

The official gitignore reference says that .gitignore is for ignoring untracked files and has no effect on files that are already tracked. A file you have committed is a tracked file, so adding it to .gitignore does not change how Git handles it.

On the other hand, .gitignore does work for new log files. When I created other.log, it did not appear in git status --short.

console
$ printf "new\n" > other.log && git status --short && rm other.log
 M .env
 M debug.log
?? .gitignore

Finding files that match .gitignore but are still tracked

To list tracked files that match an ignore rule, combine git ls-files with -c (tracked files), -i (only files matching ignore rules), and --exclude-standard (use the standard rules, including .gitignore). See the official git ls-files reference.

console
$ git ls-files -ci --exclude-standard
.env
debug.log
node_modules/left-pad/index.js

git check-ignore -v, which shows which rule matches a path, prints nothing for tracked files. The official reference states that tracked files are not subject to ignore rules, so they are not shown. With --no-index, it showed which line of .gitignore matched each path (official git check-ignore reference).

console
$ git check-ignore -v .env; echo "exit=$?"
exit=1

$ git check-ignore -v --no-index .env debug.log node_modules/left-pad/index.js
.gitignore:1:.env	.env
.gitignore:3:*.log	debug.log
.gitignore:2:node_modules/	node_modules/left-pad/index.js

Untrack files with git rm –cached

Adding --cached to git rm removes files only from the stage (the index) and leaves the files in the working tree, whether or not they have changes (see the official git rm reference). Add -r when removing a directory. It is a good idea to run with -n first to check what will be removed. With -n, the command only prints the list and changes nothing.

console
$ git rm -n -r --cached .env debug.log node_modules
rm '.env'
rm 'debug.log'
rm 'node_modules/left-pad/index.js'

$ git rm -r --cached .env debug.log node_modules
rm '.env'
rm 'debug.log'
rm 'node_modules/left-pad/index.js'

In git status, the three files now appear as deletions staged for the next commit (deleted:). The local files are still there. The contents of .env are also unchanged from the edited version.

console
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    .env
	deleted:    debug.log
	deleted:    node_modules/left-pad/index.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore


$ ls -A
.env
.git
.gitignore
app.js
debug.log
node_modules

$ cat .env
API_KEY=dummy-key-2

I also committed .gitignore. The only tracked files are now .gitignore and app.js.

console
$ git add .gitignore && git commit -q -m "追跡しないファイルを .gitignore に入れる"

$ git ls-files
.gitignore
app.js

$ git ls-files -ci --exclude-standard

After this, changing .env or debug.log shows nothing in git status, and git add -n . also prints nothing. git check-ignore -v now shows the matching rules even without --no-index.

console
$ printf "API_KEY=dummy-key-3\n" > .env && printf "even more\n" >> debug.log

$ git status
On branch main
nothing to commit, working tree clean

$ git add -n .

$ git check-ignore -v .env debug.log node_modules/left-pad/index.js
.gitignore:1:.env	.env
.gitignore:3:*.log	debug.log
.gitignore:2:node_modules/	node_modules/left-pad/index.js
Top box: a tracked file (the committed .env). Adding it to .gitignore does not help. Editing it makes it show as modified in git status, and git add . stages it. Bottom box: after running git rm --cached .env and committing (pink arrow), the file is untracked. Editing it does not show in git status, git add . does not stage it, the local file remains, and the earlier commit still contains its contents
How .gitignore behaves before and after git rm –cached

If you forget –cached, the local file is deleted too

If you run git rm without --cached, the file is removed from the stage and also deleted from disk. In another repository, where I had committed only debug.log, I ran git rm debug.log, and debug.log was gone afterward.

console
$ git rm debug.log
rm 'debug.log'

$ ls -A
.git

If you catch this before committing, run git restore --staged to cancel the staged deletion, then git restore to bring the file back from the last commit.

console
$ git restore --staged debug.log && git restore debug.log

$ cat debug.log
start
more

When you have many files to untrack, remove them all and re-add

If you have many files to untrack, another option is to untrack everything and then re-add it with git add .. Only the files that match .gitignore end up untracked. I tested this in another repository where app.js, .env, and debug.log were committed, with a .gitignore containing .env and *.log.

console
$ git rm -r -q --cached . && git add . && git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    .env
	new file:   .gitignore
	deleted:    debug.log


$ ls -A
.env
.git
.gitignore
app.js
debug.log

app.js, which did not change, does not appear in git status. With this method, a mistake in the .gitignore patterns can untrack files you did not mean to remove. Before committing, check git status and confirm that the deleted: entries are only the files you want to untrack.

The earlier commit still contains the file contents

git rm --cached only keeps the file out of future commits. The earlier commits still contain the file as it was. Passing a file name to git log showed both the commit that added .env and the commit that untracked it. With git show and a commit and file name, I could retrieve the contents from the first commit.

console
$ git log --oneline -- .env
c729766 追跡しないファイルを .gitignore に入れる
04245d9 最初のコミット

$ git show HEAD~1:.env
API_KEY=dummy-key-1
Warning

If you committed and pushed passwords, API keys, or similar secrets, running git rm --cached does not protect them. Anyone who can view the repository can still read the contents from the earlier commits. The GitHub documentation on removing sensitive data from a repository lists revoking the password or token, or replacing it with a new one, as the first step. The same page explains how to remove the data from history, but that requires coordinating with everyone who has cloned the repository.

In other clones, pulling deletes the file

I pushed the commit that untracked the files to a shared repository and pulled it in another clone. For Git, this commit deletes the files, so .env and the other files were removed from the working tree of the pulling clone.

console
$ cat .env
API_KEY=dummy-key-1

$ git pull -q --ff-only origin main

$ git log --oneline
c729766 追跡しないファイルを .gitignore に入れる
04245d9 最初のコミット

$ ls -A
.git
.gitignore
app.js

$ cat .env
cat: .env: No such file or directory

If a file that each person needs locally, such as .env, is gone, you can restore it from an earlier commit. git show returns the contents as they were when committed. Writing the output of git show to a file does not stage anything. Because the file matches .gitignore, it does not appear in git status either.

console
$ git show HEAD~1:.env > .env

$ cat .env
API_KEY=dummy-key-1

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

If you edited .env before pulling

In another clone that had not pulled yet, I edited .env and then ran git pull. The pull was aborted, and the local .env kept the edited contents.

console
$ printf "API_KEY=local-edit\n" > .env

$ git status --short
 M .env

$ git pull -q --ff-only origin main; echo "exit=$?"
error: Your local changes to the following files would be overwritten by merge:
	.env
Please commit your changes or stash them before you merge.
Aborting
exit=1

$ cat .env
API_KEY=local-edit

In this case, copy .env outside the repository, discard the edits with git checkout -- .env, and then pull. After the pull, copy the file back. The .env with your edits is restored locally, and git status --short shows nothing.

console
$ cp .env ../env-backup && git checkout -- .env && git pull -q --ff-only origin main; echo "exit=$?"
exit=0

$ cp ../env-backup .env && cat .env && git status --short
API_KEY=local-edit

​Posted by 86