Git : How to protect a directory - php

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?

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.

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?

Related

Delete settings file from GitHub repo without deleting it from every remote clone

I've inherited a PHP Weather Template Website that's updated and distributed through GitHub. The previous owner did not make a sufficient .gitignore file in the beginning, so there are a number of files that are tracked by git that shouldn't. These files include .csv databases that are updated with users' data, json data files, and a settings.php file. After adding a list of files to the .gitignore, my issue now is the best way to get these files to not be tracked by git on all of the remote clones that users are using. My first thought was to just do a git rm --cached <files>, however that removes the files from everyone, deleting everyone's data.
Next I thought to warn people to backup the files and in a week I'd issue the git rm --cached command, however with an unknown but fairly large number of users, I have no guarantee that everyone will see the message in the forums before its too late, plus surely some users will do a git pull without even looking at the forums.
For the same reasons as above, having each user issue git rm --cached probably won't work either.
Ultimately, is there any way that I can get git to stop tracking these files on every remote copy of the template, without relying on any user interaction. A simple delete-from-GitHub-but-not-from-outstanding-copies command would be great.
There is no way to do what you want. Github is not a file hosting, it's git repository hosting, it shows the content of your repositories. To remove files from Github you have to remove the files from repository (not necessary in that order: you can remove files directly from Github using web interface but then you'll have to pull changes and the files will be removed in your local repo). No way around that, sorry.
If I'm understanding your problem correctly, you want to remove the .csv files from the repository, but when someone else does a git pull their own .csv's are unaffected.
Quick Note: Do this in a separate branch or a dry run to be safe.
1) untrack all files git rm -r --cached .
2) update the .gitignore file to include all of these files.
3) add all the files back git add . (this will not include .gitignored files)
4) commit/push

Git workflow, am I doing it right?

More and more projects are starting to pile up and I want some workflow and also version control over the different projects. I'm trying to set the "right" workflow with Xampp, Git, GitDesktop and PhpStorm on a Windows 2012r2 machine.
Xampp base: d:\xampp
http://localhost = d:\xampp\htdocs
Dev repositories: d:\xampp\htdocs\repositories\dev\GitDemo
Live repositories: d:\xampp\htdocs\repositories\live\GitDemo
Live folder: d:\xampp\htdocs\GitDemo
Live URL: http://servername/GitDemo (intranet use only)
Right now I have my repositories folder inside the htdocs folder, otherwise I would need another alias/copy action to be able to see what I'm developing. But at the same time the repositories folder is exposed. How to hide this? htaccess?
I've ran git init --bare inside the live folder for this project. With GitDesktop I've created the repository for GitDemo inside d:\xampp\htdocs\repositories\dev.
Using PhpStorm I've created a project based upon local files and pointed it towards d:\xampp\htdocs\repositories\dev\GitDemo. I'm able to see the changes made using git status, add them using git add . and commit them succesfully with git commit -m "my commit..".
I've created a remote server git remote add live master and created a post-receive to checkout the files inside d:\xampp\htdocs\repositories\live\GitDemo to d:\xampp\htdocs\GitDemo.
This all feels like a "ton" of work to set up initially and somewhat redundant (having the same files in 3 locations).
Is this the ideal way to set this up, or do you suggest an alternative approach? Thanks for sharing your thoughts!
I've been thinking about the most logical solution (to my opinion at this moment). Here's how I solved my unclear items as described:
I've optimized it by:
Bringing the repositories directory outside htdocs to d:\repositories\dev\ and d:\repositories\live.
I've set up a symlink to http://localhost/dev/GitDemo that links to
d:\repositories\dev\GitDemo. In this way I don't need to place the
repositories folder inside the htdocs folder and still benefit from
Apache being able to serve the content, which actually resides outside the htdocs folder.
The live version is now placed at http://localhost/GitDemo and with a post-receive hook it gets deployed from d:\repositories\live\GitDemo.git to d:\xampp\htdocs\GitDemo.
If you think I made a mistake or have mistaken something from the way it's supposed to be, please correct me as I'm still not sure this is the correct way but at least it seems like it to me.

Forever exclude, ignore files in existing repo using git

I'm working with a large PHP project in git and having trouble with my working copy because of a couple files, including the .htaccess, .gitignore and database configuration files getting overwritten or wanting to be staged/committed.
The problem I'm running into is that every time I try to switch branches, I'm bothered to either stash my changes or told that I can't merge because files are not uptodate. I've tried updating my exclude to the following:
.htaccess
.gitignore
./application/config/vagrant/config.php
./application/config/vagrant/database.php
Along with this, I've also tried to set these files to assume-unchanged - which is where i get the issues trying to switch branches and it telling me the files are not uptodate.
What I'd like to happen is for those files to be completely ignored forever. Whenever I switch branches, I don't want them to be merged, I just want them to essentially be removed from the repo without affecting any of the other users that work on the project.
From your description of the problem, it seems that the offending files
.htaccess
.gitignore
./application/config/vagrant/config.php
./application/config/vagrant/database.php
are already being tracked; perhaps you or one of your collaborators started tracking them by mistake a long time ago...
If you want Git to be able to ignore those files, you first need to tell Git to stop tracking them by running
git rm --cached <paths>
Then, and after adding appropriate entries in your .gitignore file, you'll be able to ignore those files, as desired.

Do a "git diff" between two directories due to website owner making live changes

I have a project I'm working on which I've set up in a git repo.
Since I put my latest version live, the website owner has made some changes to the working/content by overwriting it directly.
Obviously these changes were made outside of version control. I suppose I could overwrite the entire contents of my repo, then commit. That should work... but I don't really like the idea of doing that, especially if there's been any replacement of correct code/html-structure with stuff that's incorrect or bad practice.
What I'd like to do is dump the website from live into another directory and do a recursive diff so I can only overwrite those files which have changed (any correct any issues if there are any)
As in if I just overwrite what's in my git repo, then run git status?
No, you don't have to overwrite anything.
You can do a diff between:
the index representing your site in its latest versioned state (in the git repo)
a dump of the current live site (a copy done in a different folder, not under version control)
You can then do (using git options) a:
git diff --git-dir=/path/to/repo/.git --work-tree=/path/to/dump .
You actually can execute that command from any folder: it will look for the git index and for the right working tree.
The OP Tom Busby adds in the comments:
In the end I solved it by simply overwriting the .git folder in my dump directory with the one from my git repo and running git diff.
VonC's answer is excellent, but I believe you could also just use the diff command (a built-in command on most operating systems). This would also ensure that "gitignored" files were diffed (though VonC's might already do that).
Assuming the website in /www/mysite and a fresh clone in /git/mysite you could just do:
diff /www/mysite /git/mysite
Yes We can compare two directories very easily
simply using this command on Shell/Terminal
git diffdir any-branch-name any-other-branch-name

Doctrine SVN missing folder structure

After checking Out the Doctrine SVN from GitHub:
http://svn.github.com/doctrine/doctrine2.git
There are missing folders in lib/vendor/doctrine-common (Maybe others too)
My checked out version has nothing inside this folder (only the svn folder), yet the online folder structure shows the folders; lib & tests folders plus some build.xml files etc
Also, it appears that checking out the DABL SVN also fails:
http://svn.github.com/doctrine/dbal.git
Has anyone else had these problems or able to explain what might be wrong with these repos? (The downloadable version appears to be a different structure again and excludes files that I would like to use to test this library out using the tutorial at http://www.doctrine-project.org/docs/orm/2.0/en/reference/introduction.html i.e. the Sandbox files ).
Regards
My checked out version has nothing inside this folder (only the svn folder), yet the online folder structure shows the folders; lib & tests folders plus some build.xml files etc
That's because it's using a Git submodule, I suppose. You'll have to pull the Git submodule yourself, as far as I'm aware, though I don't use git-svn. Probably it will be fixed with:
git submodule update
To get the entire repo I downloaded a git client that uses MingW and used bash to download the repo... to get the GIT sub-modules you must add --recursive so that it downloads the entire tree structure recursively grabbing the sub-module classes.

Categories