375

I have accidentally committed the .idea/ directory into git. This is causing conflicts everywhere else I need to checkout my repo. I was wondering how do I remove these files from the remote?

I still need these files locally since the Intellij IDE needs them. I just don't want them in the remote. I have added the directory .idea/ to my .gitignore and committed and pushed this file into remote. This seems to have no effect during my checkout on my other machine though. I still get the error message:

error: The following untracked working tree files would be overwritten by checkout:
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/uiDesigner.xml
.idea/vcs.xml
.idea/workspace.xml
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
sethu
  • 8,181
  • 7
  • 39
  • 65
  • 11
    You can help avoid this in the future by reviewing your commits. I prefer to run `commit` with `-v` to show the diff in your editor when you're writing the commit. It helps prevent this kind of mistake as well as helping you focus your commit message on what you changed. – Daenyth Jun 20 '12 at 16:44
  • Daenyth is right. Plus Git by default requires you to add files to the list of committed changes, so take use of that and do not commit everything at once. Otherwise you will pollute your repository as it happened by adding `.idea` to it. – Tadeck Jun 20 '12 at 16:47
  • 1
    All but two of the files in .idea should be in source control: http://devnet.jetbrains.com/docs/DOC-1186 – cja Dec 05 '13 at 11:06
  • I have found IntelliJ .ignore plugin very useful when you want to manage all the files that should not be committed to git: https://plugins.jetbrains.com/plugin/7495?pr=idea – panza Jun 05 '16 at 10:55
  • Hi , what is the harmful of that if we have .idea in remote repository? – milad salimi May 17 '20 at 08:10
  • .idea is a directory that is used by intellij for a lot of things. If you are working in a team, or working on the project on another computer, checking in that diirectory will cause issues. It's not a part of the source code you wrote and hence managing conflicts would be very hard. More info here - https://rider-support.jetbrains.com/hc/en-us/articles/207097529-What-is-the-idea-folder- – sethu May 31 '20 at 20:36

6 Answers6

791

Add .idea directory to the list of ignored files

First, add it to .gitignore, so it is not accidentally committed by you (or someone else) again:

.idea

Remove it from repository

Second, remove the directory only from the repository, but do not delete it locally. To achieve that, do what is listed here:

Remove a file from a Git repository without deleting it from the local filesystem

Send the change to others

Third, commit the .gitignore file and the removal of .idea from the repository. After that push it to the remote(s).

Summary

The full process would look like this:

$ echo '.idea' >> .gitignore
$ git rm -r --cached .idea
$ git add .gitignore
$ git commit -m '(some message stating you added .idea to ignored entries)'
$ git push

(optionally you can replace last line with git push some_remote, where some_remote is the name of the remote you want to push to)

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • 5
    Note: If you get the message `fatal: pathspec '.idea' did not match any files` when you run `git rm -r --cached .idea`, delete your .idea folder, pull from github and then try again. – annedroiid Dec 07 '16 at 04:37
  • If you get @annedroiid error of 'did not match any files', you can try and add its path. Like when I was deleting the database.yml file, I simply did 'git rm -r --cached config/database.yml' – Kaka Ruto Mar 15 '17 at 23:19
  • Some files in the .idea directory should be checked in to your repo. See https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore for JetBrains suggestion on how your .gitignore should look like. – tokenizer_fsj Jul 20 '18 at 15:14
  • 3
    I cant get this folder out of our repo for the life of me... switching branches keeps re-tracking it... people have ran this on multiple branches and any time you switch branches... git thinks it needs to ... at some point ... delete your .idea folder so.. in short... switching branches now is basically a settings wipe for us. I tried running it on master... then merging into all branches.. but stills is wiping settings. – nawlbergs Aug 29 '18 at 15:34
  • 2
    Do we have a permanent solution for this to set to IntelliJ to NEVER add `.idea` folder to commit files? – Serhat Jan 22 '20 at 07:56
  • Using this answer now for the millionth time as reference. Unfortunately I can't upvote more than once :) – Micromegas Dec 02 '20 at 18:06
114

You can remove it from the repo and commit the change.

git rm .idea/ -r --cached
git add -u .idea/
git commit -m "Removed the .idea folder"

After that, you can push it to the remote and every checkout/clone after that will be ok.

Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
7

Its better to perform this over Master branch

Edit .gitignore file. Add the below line in it.

.idea

Remove .idea folder from remote repo. using below command.

git rm -r --cached .idea

For more info. reference: Removing Files from a Git Repository Without Actually Deleting Them

Stage .gitignore file. Using below command

git add .gitignore

Commit

git commit -m 'Removed .idea folder'

Push to remote

git push origin master

rahulnikhare
  • 1,362
  • 1
  • 18
  • 25
1

if you haven't push to git

git rm .idea/ -r --cached
git add -u .idea/
git commit -m "Removed the .idea folder"

if "git rm .idea/ -r --cached" throw an error "fatal: pathspec '.idea' did not match any files" and already push to git with .idea on it.

git push origin --delete remoteBranchName
git add . //Everything is up to date
git commit -m "Removed the .idea folder" //Nothing to commit
git push origin remoteBranchName
Mark Anthony Libres
  • 906
  • 1
  • 7
  • 14
0

You should add a .gitignore file to your project and add /.idea to it. You should add each directory / file in one line.

If you have an existing .gitignore file then you should simply add a new line to the file and put /.idea to the new line.

After that run git rm -r --cached .idea command.

If you faced an error you can run git rm -r -f --cached .idea command. After all run git add . and then git commit -m "Removed .idea directory and added a .gitignore file" and finally push the changes by running git push command.

Hossein
  • 2,592
  • 4
  • 23
  • 39
0

To avoid this problem recurring, I found it useful to have a global gitignore file containing .idea, e.g.:

$ echo '.idea' >> ~/.gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global

Another advantage of this approach is that you don't need to "pollute" your project's .gitignore file with details of your IDE. OTOH, you may also choose to add .idea to the project's .gitignore so other contributors to the project won't hit the problem.

glyn
  • 1,180
  • 8
  • 19