Accidentally committed .idea directory files into git
GitGit Problem Overview
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
Git Solutions
Solution 1 - Git
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:
> https://stackoverflow.com/q/1143796/548696
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)
Solution 2 - Git
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.
Solution 3 - Git
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
Solution 4 - Git
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.
Solution 5 - Git
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
Solution 6 - Git
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.