When IntelliJ Loses Its Mind, Run This

Occasionally, IntelliJ goes haywire and won't run your project or tests. Next time this happens, close IntelliJ, run the script below in your project directory, and then open IntelliJ and reimport the project.

The script copies your .idea folder to /tmp, runs git clean -xdf to remove all files not checked into git, and then copies the important files back into a new .idea folder so you don't lose things like database configurations, dictionaries, and project settings. Your old .idea folder remains in /tmp until the next time you run the script or the OS deletes it in case you need something else.


#!/bin/bash
if ! git diff-index --quiet HEAD; then
echo "Uncommited files detected, commit them and try again. Exiting."
exit 1
fi
set -o errexit -o noclobber -o nounset
idea="./.idea"
tmpIdea="/tmp/.idea"
items=("$tmpIdea"/codeStyles "$tmpIdea"/dataSources "$tmpIdea"/dictionaries "$tmpIdea"/inspectionProfiles "$tmpIdea"/dataSources*.xml "$tmpIdea"/workspace.xml)
rm -rf $tmpIdea
mkdir $tmpIdea
mv $idea /tmp
git clean -xdf
mkdir $idea
for i in ${items[*]}; do
if [ -d "$i" ]; then cp -r "$i" $idea; fi
if [ -f "$i" ]; then cp "$i" $idea; fi
done

Stay up to date

Get notified when I publish. Unsubscribe at any time.