IntelliJ IDEA
version control
remove file
Git
programming tips

Remove a file from Git version control in IntelliJ IDEA

Codemia Knowledge Hub ·

Remove the file from the Git index, add it to .gitignore, and commit the deletion. The file stays in your working directory but is no longer tracked. This is what you want for build artifacts and other temporary files that should never have been committed.

Tracked files, .gitignore and the index

  • Tracked vs. untracked: tracked files are monitored for changes; untracked files are not. Removing a file from version control makes it untracked but leaves it in the working directory.
  • .gitignore: patterns in .gitignore stop matching files from being staged for commit.
  • Index (staging area): where files go before a commit. To stop tracking a file, it has to be removed from the index.

Steps in IntelliJ IDEA

  1. Open the project and find the file in the Project view.
  2. Right-click the file, go to Git (or your VCS, such as SVN) and choose Delete. This removes the file from the index while leaving it in the working directory.
  3. To keep the file from being tracked again, add it to the .gitignore file at the root of the repository, and commit the .gitignore change.
  4. Open the Commit window. The file appears under Deleted. Tick its checkbox, write a commit message and commit.
  5. If you use a remote repository, push the commit.

A .gitignore entry for a single file and for a whole build directory:

text
config/local.properties
build/

The same steps from IntelliJ's terminal (git rm --cached keeps the file on disk):

bash
1git rm --cached config/local.properties   # add -r for a directory
2echo "config/local.properties" >> .gitignore
3git add .gitignore
4git commit -m "Stop tracking config/local.properties"
5git push

Why the file is still tracked

  • Deleting alone is not ignoring. A file stays excluded only when the deletion is committed and the file is in .gitignore.
  • The deletion was never committed. If the file does not appear as deleted, make sure it was committed as a deletion, not just removed locally.
  • .gitignore was updated but changes are still tracked. The file must first be removed from the index, with git rm --cached <file> on the command line or the equivalent in IntelliJ IDEA. .gitignore has no effect on files that are already tracked.

History and collaborators

The file's history is preserved in earlier commits, so you can still view or recover old versions. Tell your team about the change, especially for commonly used files or configuration.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions