Forever exclude, ignore files in existing repo using git - php

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.

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 & Laravel stopping certain files

I've been running a project written in Laravel which has been fun to use.
The setup I use is the vagrant box Homestead configuration so I do the majority of my work on my local machine and push up to the development server once its ready to go. During the installation I had to push up the logs & vendor folder for it to work properly but now I'm at a stage where every commit I do via the command line includes storage/logs/laravel.log which when I then pull down it asks me to stash/commit on the server because they're different.
I've added it to the .gitignore file in the root directory of the project and it looks like this:
/node_modules
/public/storage
/.idea
Homestead.json
Homestead.yaml
/storage/logs/
/vendor/
Vendor doesn't cause me any problems unless I make changes to it so its not much of a bother, its just the logs which will not stop going up. If I use a GUI tool, I can manually tell it not to go up but I want to get it to the point that I can use the terminal to push it up and not worry about logs need stashing on the server.
I believe this is the same for the .env so I imagine a solution will work for both. I have also noticed that PHPStorm have said they're ignored but tracked with git if that helps.
If you take a look at the Laravel repo on GitHub, you'll find the following .gitignore file in the storage directory:
https://github.com/laravel/laravel/blob/master/storage/logs/.gitignore
This comes with the default installation to mark the logs directory as ignored. If you've deleted this by mistake, you should be able to reinstate it and resolve the issue you're having.
Just as importantly though, your workflow isn't following best practice. With respect to "Vendor doesn't cause me and problems unless i make changes to it" - you should never make changes to your vendor directory. This folder is home to third-party packages and plugins, modifying them directly causes multiple issues, chief amongst them:
You can no longer update a modified package without breaking your application.
Other developers won't be able to replicate your installation easily.
In fact, the vendor directory shouldn't be versioned at all. The best way to handle the files within it is using a package manager, like Composer, to do it all for you. This means you can easily switch between different versions of your packages and, by versioning only the composer files, other developers can run composer install or composer update to synchronise their development environment to yours quickly and accurately.

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

Git : How to protect a directory

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?

Remove files from Bazaar

I'm using Bazaar (bzr) to keep source code for a website updated, but we've ran into a problem when we remove files from version control. The files we are removing are ones we never intended to version to begin with.
When this happens we use bzr rm --keep to remove the file from version control, but keep the file in the file system. Doing a bzr push or bzr pull results in the removed file(s) being removed on the other branches (other sites that use our code)
We need a way to make sure that a bzr push or bzr pull doesn't actually remove those from the working copy. Anyone have any ideas?
You need to run bzr rm --keep in all locations manually before updating them with pull/push. There is no other way unfortunately.

Categories