1

I am working on a php web application where I need a directory named "library" to always be left untouched by git.

The reason is that this directory is used for the storage of other files and folders that the user might upload from within my php application to the server. So, it contains items on my local machine and different items on the staging server that i am using, which should not be deleted, ever.

I understand that i can .gitignore the "library" directory, but i am reading warnings about git possibly deleting gitignored items, at some point.

Is there any method for explicitly protecting a directory or a file?

Renaissance
  • 798
  • 5
  • 15
  • 1
    "but i am reading warnings about git possibly deleting gitignored items" citation needed. – PeeHaa Sep 28 '13 at 22:38
  • .gitignore is the only way, check whether you have mentioned correctly. https://github.com/github/gitignore. Can you let me what you have mentioned in ".gitignore"? – Viji Sep 28 '13 at 22:44
  • My .gitgnore only contains "library/". Is this correct? – eyeprotocol Sep 28 '13 at 22:46
  • Syntax is correct. Are you getting this warning, while you do git push? – Viji Sep 29 '13 at 00:42

2 Answers2

2

Git will only ever delete a directory—or files for that matter—if those are actually part of the repository at some point. If none of your branches contain those files, Git won’t touch them at all though. And even if it does; no data is lost.

To understand that, we need to understand what happens on a checkout in Git. Suppose we are on branch A and want to check out branch B. What Git does is remove all files that are contained in branch A and are clean (i.e. they don’t have any uncommitted changes). Next, Git will create all files from branch B. Now, if branch A tracked a file but branch B doesn’t, then this results in that file being removed on the checkout. The same applies to directories (because Git only tracks files, and directories are created/removed as needed). But when we switch back to branch A, the file is being restored again.

So to summarize this; a file is only removed when it is part of the Git repository and the new checkout target does not contain that file. Otherwise, it is left where it is (as Git doesn’t even know about it).

So if you just don’t want any of the files in that directory to be tracked within Git, add it to the .gitignore and don’t add it manually to the repository.

poke
  • 369,085
  • 72
  • 557
  • 602
0

You can add a .gitkeep file to the library directory that will tell git to keep that directory in version control. Just don't commit any files to library that are environment specific.

What are the differences between .gitignore and .gitkeep?

Community
  • 1
  • 1
eoconnell
  • 101
  • 4