115

VCS has an Add option (Git Add) but seems to lack Git Remove.

What's the idiomatic way to Git Remove with VCS?

Jire
  • 9,680
  • 14
  • 52
  • 87
  • 4
    Seems like IntelliJ provides no UI for this task, and we unfortunately have to resort to doing `git rm -r --cached` CLI on a terminal. – Elvin Dec 17 '22 at 17:37

8 Answers8

163

In the terminal, use git rm --cached -r .idea/. This will remove the files from the GIT index, but leave the files locally.

Kick_the_BUCKET
  • 1,955
  • 2
  • 12
  • 8
  • 69
    Why no ui button? – Alex78191 Jan 22 '19 at 13:29
  • 1
    perfect! I used git rm --cached -r ./ in home directory of project for total delete from git and add them just as I wanted. Thanks mate! :) – Ivan Kolyhalov Sep 11 '19 at 12:45
  • 8
    This should be the selected answer as it doesn't delete the file completely, just unstages it from GIT. Thanks! – Richard KeMiKaL GeNeRaL Denton Mar 06 '20 at 14:02
  • 4
    This does not answer the question asked. The question was how to, through IntelliJ's VCS tools, untrack a file. This answer uses git tools to accomplish the task, not IntelliJ. @jyapx's answer should be marked as the correct answer. Using `Rollback` through IntelliJ's VCS tools will remove a newly added file (green file) from git tracking (red file). – JM_24 Jan 11 '23 at 23:30
  • This did not work for me, I was getting `fatal: pathspec 'idea/' did not match any files` but `git restore --staged` did the trick to remove all staged files in git (but keep them locally). – Balu Apr 13 '23 at 13:02
  • @Balu I got the same error, your suggestion almost worked, I had to add a dot (`.`) at the end otherwise I got `fatal: you must specify path(s) to restore`, so it would correctly look like this: `git restore --staged .` – benomatis Jul 03 '23 at 09:00
130

To change a file from green (staged) to red (untracked) using Intellij:

  1. Right click the file(s)
  2. Hover over Git (the git pane will expand)
  3. Click Rollback... (in older versions Revert)
  4. Check that Delete local copies of added files is not checked and click the Rollback button

This will not delete the file, only unstage it (remove it from git's index).

Dan Berindei
  • 7,054
  • 3
  • 41
  • 48
jyapx
  • 2,009
  • 2
  • 15
  • 18
  • 5
    @AksharPatel That is not true. If the file is green (added using git add ) that means the file is new, not modified. If you untrack it using the "revert" option in Intellij git will only untrack the file, but the file will remain unaffected in your project. If the file was modified - which appears as blue in Intellij - only then you will lose any file modifications after you git revert. – jyapx Sep 04 '19 at 16:10
  • @Jyapx Ah. That makes sense. By changes, I did mean the blue ones and that is why I posted a comment rather than editing the answer. – Akshar Patel Sep 04 '19 at 16:20
30

Given your project is linked to a git repo already, you can just use the standard IntelliJ feature to "delete" the file.

In the project pane, focus the file and hit Delete key. The "Safe Delete" popup will appear, click OK.

Now observe under 9: Version Control -> Local Changes that the file is listed there in "grey" - when you commit and push to your git repo, the file will be deleted on the current branch.

Edit: if these are IntelliJ files, this becomes more difficult.

First, close IntelliJ, make a list of the exact files you want to delete from repo, and take a copy of those files on your local file system.

Then use git rm to remove them and then commit.

Next step, add a .gitignore file to ignore local IntelliJ files. A good start is *.iml and .idea.

Finally, restore the files that you copied up and restart IntelliJ.

Pang
  • 9,564
  • 146
  • 81
  • 122
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • Unfortunately most of the files I want to remove from the Git repo are IntelliJ project files. – Jire Feb 07 '16 at 17:09
  • 15
    This will not only remove the file from VCS, it will delete it from your local disk as well. – Chris Sep 26 '19 at 14:56
  • One interesting quirk is that sometimes when you use intellij to delete a file, intellij seems to be smart enough to recognize when you've actually copied the file, and so it can show up as a 'rename' instead of a deletion from git. – Adam Wise Mar 09 '21 at 16:41
10

You may have ADDed too quickly a file to Git, thus your file is green (staged) rather than red (untracked).

The right thing is to use command lines in your terminal. git status will hint to make git reset HEAD <file> to untrack your file.

Then your file will be untracked again (red color).

In case of doubt, you can save before your current work with the custom Intellij repo using the menu VCS -> Local History-> Put Label

Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74
5

You can use External Tools to add the funcation to IDEA.

As shown in the figure, then you can right-click any file/folder and select External Tools> git rm --cache -r

snapshot

ipcjs
  • 2,082
  • 2
  • 17
  • 20
2

If you have current local changes on your change set, and you accidentally added a file to Git, it will turn green on your IDE.

To remove it from git, I just

right click on the file>Git>Rollback

voila, the file will turn Grey will not be included/added to git when you commit.

(*Make sure you have a copy of the file somewhere as a backup.)

bherto39
  • 1,516
  • 3
  • 14
  • 29
1

From: https://stackoverflow.com/a/43648056/3198983

  • Go to File Menu-> Settings | Version Control | Confirmation, then check the Show options before adding to version control setting under the When files are created section. Alternatively, you could check Do not add. It is mandatory that you do not check Add silently.
  • Delete the file you don't want to be tracked by VCS.
  • Press Ctrl + Z to undo the removal of the file. If IDE shows a popup window which lets you choose whether or not to add the new file to VCS, click No.
  • Commit your local changes, and the files are removed from VCS.
JesusIniesta
  • 10,412
  • 1
  • 35
  • 28
-3
  1. Copy your file to local disk.
  2. Delete it from project.
  3. Commit your changes.
  4. Add this file or folder to your .gitignore rules.
  5. Insert the file again and reject suggestion to add it to Git.
Zon
  • 18,610
  • 7
  • 91
  • 99