6

I have some files that I don't want to share between my repositories. I added them to .gitignore at the root of my project :

# Specifies intentionally untracked files to ignore when using Git
# http://git-scm.com/docs/gitignore
# ...
.idea/

And yet I keep getting this error when pulling remote changes using git pull :

error: Your local changes to the following files would be overwritten by merge :
      .idea/workspace.xml
Please, commit your changes or stash them before you can merge.
Aborting

I have seen some questions about this error message, but they were about overwriting the files. I want to ignore the changes (I don't want git to track them at all and I don't want git to change my local files).

What am I doing wrong ? Maybe the problem is I created the .gitignore file after I had already committed the files I wanted to ignore, and I need to remove them from the repo somehow...?

yannick1976
  • 10,171
  • 2
  • 19
  • 27

3 Answers3

7

If you don't want to track them, you need to remove them from the history first, and push that removal

git rm --cached -- afile
git add -u .
git commit
git push

As soon as the file is removed locally (the --cached keeps it on your disk), your .gitignore will work.
But any git pull would get back that file if it was versioned on the remote repo side. Hence the need to push (publish) that removal, provided that you are sure every other user should not see that file anymore as well.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It worked, thanks ! I un-tracked the whole directory using `git rm --cached -r -- .idea` – yannick1976 Nov 01 '15 at 12:12
  • i can't remove ignored file from git. git return: "fatal: pathspec 'application/config/database.php' did not match any files" – levye Sep 26 '16 at 12:37
  • @levye That means the file was not tracked in the first place. – VonC Sep 26 '16 at 12:41
  • Doesn't work for me: *Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. 'git pull ...') before pushing again* which I can't do because *Automatic merge failed; fix conflicts and then commit the result* – ᴍᴇʜᴏᴠ Nov 05 '16 at 12:21
  • @TheSexiestManinJamaica yes, that is a different issue: you need to be able to push. Any chance you could pull and resolve your conflicts? – VonC Nov 05 '16 at 12:23
  • @VonC I ended up logging in to the server that has the latest code and removing the files (and commiting that change) from there – ᴍᴇʜᴏᴠ Nov 05 '16 at 12:33
  • @TheSexiestManinJamaica that is a good solution. You can now clone again the repo, and go on working locally from there. – VonC Nov 05 '16 at 12:34
1

.gitignore - Specifies intentionally untracked files to ignore

In this case your .idea directory is already tracked in git. So you need to remove it from repo first.

After that if you add .idea folder into your .gitignore file, it won't show up among untracked files.

sdayal
  • 1,126
  • 10
  • 16
0

Don't forgot to commit and push your gitignore file.