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
.gitignorestop 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
- Open the project and find the file in the Project view.
- Right-click the file, go to
Git(or your VCS, such as SVN) and chooseDelete. This removes the file from the index while leaving it in the working directory. - To keep the file from being tracked again, add it to the
.gitignorefile at the root of the repository, and commit the.gitignorechange. - Open the
Commitwindow. The file appears underDeleted. Tick its checkbox, write a commit message and commit. - If you use a remote repository, push the commit.
A .gitignore entry for a single file and for a whole build directory:
The same steps from IntelliJ's terminal (git rm --cached keeps the file on disk):
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..gitignorehas 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.
Related reading
- Remove folder and its contents from Git/GitHub's history
- Remove large .pack file created by Git
- Remove last commit from remote Git repository
- Remove local git tags that are no longer on the remote repository
- Remove refs/original/heads/master from git repo after filter-branch --tree-filter?
- Remove sensitive files and their commits from Git history
- Remove specific commit
- Remove tracking branches no longer on remote
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.