In Git you have 3 types of files: tracked, ignored and untracked.
Tracked ones are simple – they’re just files you’ve added to the repository. This might be your application’s code. Ignored ones are also simple – if you don’t want to have any file you can add it to
.gitignore file and you will have it locally but not in the repository. This might be any vendors you can easily install and don’t need to store them in your repository.
But untracked files are a little bit more complicated. These are files you’ve just created and didn’t specify what to do with them. Are those useful and should be added, or are they just temporary files or vendors? They might be also autogenerated files and this post is really about them.
I’m using PropelORM in my daily work. It can auto generate models from XML definition. The point is they are not automatically added to the Git repository, at least not when used in terminal, even in PhpStorm IDE. I forgot about adding them to the repository several times. Thankfully models are auto generated during deploy so, on the production, there was no error. But still, I added some models and forgot to add them to the repository, so every developer had to autogenerate them himself (or herself).
So, to avoid further problems I searched the web for any automatic way to add them to Git, or, at least, remember myself about untracked model files. I’ve found git hooks.
Git hook is an easy script that can be launched when the specific action is fired. In my case, I had to use a pre-commit hook that checks the repository for any untracked files from
Model directories. The script is very simple and looks like this:
# hook useful when you use Propel ORM and forget to add autogenerated model files
if git ls-files --others --exclude-standard | grep -q 'Bundle/Model';
echo 'There are some Propel models not added to git:'
git ls-files --others --exclude-standard | grep 'Bundle/Model'
If you want you can find it here
It works very simply: search any untracked files that are in location contains
Bundle/Model and if so echo some message, display those files and cancel commit. Easy as that 😉
It works in the terminal as well as in PhpStorm IDE:
Thanks to this simple script I haven’t forget about models ever since 🙂
If you want to read more about git hooks and see some examples go here or check sample files in
.git/hooks directory of your project. If you have some hooks already or write a new one let me know in the comments!
Want more tech meat?
- Self code review?
- Handy PhpStorm shortcuts that make your life easier
- Adding a new Logger to Laravel
- [Edit] How to deploy Symfony app with Capistrano 3 on cheap OVH VPS
- How to make cool progressbar in Symfony command?
- Using PHP5.6 and PHP7.1 at the same time on OS X!
- Keep your logs short with logrotate
- First attempt to Selenium and PHPUnit
- Squash – simple way to cleaner git repository
- A few words about Git hooks