How can I remove a file from Git within IntelliJ VCS?

GitIntellij IdeaVersion Control

Git Problem Overview


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

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

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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).

Solution 3 - Git

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.

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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.)

Solution 7 - Git

  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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJireView Question on Stackoverflow
Solution 1 - GitKick_the_BUCKETView Answer on Stackoverflow
Solution 2 - GitjyapxView Answer on Stackoverflow
Solution 3 - GitvikingsteveView Answer on Stackoverflow
Solution 4 - GitNicolas ZozolView Answer on Stackoverflow
Solution 5 - GitipcjsView Answer on Stackoverflow
Solution 6 - Gitbherto39View Answer on Stackoverflow
Solution 7 - GitZonView Answer on Stackoverflow