378

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
7
  • 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, 2012 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, 2012 at 16:47
  • 1
    All but two of the files in .idea should be in source control: devnet.jetbrains.com/docs/DOC-1186
    – cja
    Dec 5, 2013 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: plugins.jetbrains.com/plugin/7495?pr=idea
    – panza
    Jun 5, 2016 at 10:55
  • Hi , what is the harmful of that if we have .idea in remote repository? May 17, 2020 at 8:10

6 Answers 6

794

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)

6
  • 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 7, 2016 at 4: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, 2017 at 23:19
  • Some files in the .idea directory should be checked in to your repo. See github.com/github/gitignore/blob/master/Global/… for JetBrains suggestion on how your .gitignore should look like. Jul 20, 2018 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, 2018 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, 2020 at 7:56
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.

3
  • 12
    This worked for me, as opposed to the accepted answer. Maybe it was teh git add -u that made the difference. Sep 23, 2016 at 8:24
  • You don't need the 2nd line.
    – kjdion84
    Apr 20, 2017 at 22:54
  • As the others said, i didn't run second line. Worked like a charm! thanks
    – blackkara
    Aug 4, 2017 at 11:34
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

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.