-2

I am new here and I need some help. I have a website with this structure:

  • folder1
  • folder2
  • folder3
  • folder4
  • folder5
  • index.php
  • .gitignore

In folder2 there is a config.php file. How can I ignore that file? I tried to edit .gitignore file and I add folder2/config.php but git doesn't ignore it.

I also tried from Terminal and Source Tree but without success?

Can you please tell me how can I ignore a specific file from a folder?

Many thanks in advance guys!

  • Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – 1615903 Dec 05 '17 at 06:07
  • Have you get the answer which helps you solve the problem? If yes, you can mark it as answer. And it will help others who have similar questions. – Marina Liu Dec 21 '17 at 09:18

2 Answers2

1

If the file is already part of git repo, then you need to remove it from git to prevent from further tracking. You can do it by using following command

git rm --cached folder2/config.php
git commit -m "Removed config file from repo"
git push

As for your question in comment, create and add config.php.dist and add it to git

cp folder2/config.php folder2/config.php.dist
git add folder2/config.php.dist
git commit - m 'adding config.dist to hold bsic config'
git push

See my other answer for more detail. how to specify paths in .gitignore?

Thanks!

Azhar Khattak
  • 835
  • 8
  • 11
  • I don't want to remove it, I just want to ignore it because it's possible that in the future to make some changes or to add a global variable or something – Marius Mazilu Dec 04 '17 at 21:00
  • You can create a new file e.g. `folder2/config.php.dist` and copy current contents to this new file, yes add this new file to git. Later on when you need or want to change this you can just copy/modify the contents from `folder2/config.php.dist` to/from original config. This is how frameworks handle config files. https://stackoverflow.com/questions/16843080/what-does-dist-used-as-an-extension-of-some-source-code-file-mean Then remove config.php from git only as I mentioned above. Do note `--cached`, this will remove this from git and not from local. Thanks! – Azhar Khattak Dec 04 '17 at 21:07
  • I have edited the answer w.r.t. to your question in comment Thanks! – Azhar Khattak Dec 04 '17 at 21:15
0

If you already have a file checked in, Git will not ignore the file if you add a rule later. You must untrack the file first, by running the following command in your terminal:

git rm --cached FILENAME

git commit -m "Any message"

After that you will not see any changes on that file from git diff or git status.

Adrian
  • 5
  • 5