-2

There are two files tracked by Git:

config/template.json
config/test.json

I modified them and want to ignore them forever. I tried to do that by adding them to the root .gitignore:

/config
config/template.json
config/test.json

And since it did not work, also to the config in file .gitignore:

template.json
test.json

Problem: when I use git pull, it says:

error: Your local changes to the following files would be overwritten by merge: \

   config/template.json \
   config/test.json \

Please, commit your changes or stash them before you can merge.
Aborting

I want my changes to stay local and be sent to the remote repository. What's going on? Did I not ignore these files? What to do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shlomo
  • 3,880
  • 8
  • 50
  • 82

2 Answers2

4

It is explained in the first paragraph of the documentation:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.

When you add a file to .gitignore you have to also remove it from the repository using the command:

git rm --cached filename
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Thanks I just removed the files as specified. The error msg stays on git pull. What else to do? – Shlomo Apr 19 '16 at 09:13
  • There is probably a conflict. You removed the file, somebody else changed it. If this is the case then you have to resolve the conflict using your resolution (remove the file) and commit. – axiac Apr 19 '16 at 09:15
  • There are no incomfing changes to these files. The options are commit or stash. If I commit the files, will they be readded? – Shlomo Apr 19 '16 at 09:21
  • You must not pull if your working tree is not clean. Always commit before pulling. Or you can stash the changes, pull, then apply the stashed modifications to continue from where you left. I recommend you to use a GUI git client; it's easier to understand what happens this way. – axiac Apr 19 '16 at 09:38
  • Thanks. I usually used git integrated with a IDE, but to really understand whats going on I moved to the terminal. I could not solve this issue, so I recloned the repo and set it up clean. – Shlomo Apr 19 '16 at 09:40
1

Once files are committed to Git they are tracked, even if you later ignore them. The pragmatic approach for dealing with this is to stash the changes and then push, pull, etc. as needed and pop the changes.

The permanent solution is to remove the files from source control all altogether and keep them listed in .gitignore so they are not added back again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
  • Thanks. I just `git rm --cached config/test.json` but it stays the same error message on git pull. – Shlomo Apr 19 '16 at 09:12