Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Category: Programming Page 2 of 20

Reset all git submodules

Here is a quick snippet that will reset all your git submodules to their most recent commit. Handy when something has happened such as a file permissions change that unintentionally affected the submodules.

git submodule foreach --recursive git reset --hard

Compare a single file between git branches

Comparing a file between two git branches is quite easy. You can do it with both diff and difftool.

Straight diff

git diff BRANCH_ONE BRANCH_TWO -- FILE

Using difftool

git difftool BRANCH_ONE BRANCH_TWO -- FILE

Make an empty git commit

Occassionally I have need to mark a specific moment in a git repository. Often this mark is to call out a deployment point. If everything has already been committed I will use an “empty commit” as a marker. Git makes this quite easy and in fact it is built into the commit command.

Run the following and your commit editor of choice will open to provide the commit message:

git commit --allow-empty

Alternatively you can provide the commit message inline with the standard -m

git commit --allow-empty -m "New commit message"

Quick recursive PHP file syntax check

I find it handy to run a PHP syntax (lint) check on files after resolving a merge conflict in git.

Here is a handy snippet that will run find every file in the current and sub-directories and run them through the PHP command line syntax checker.

find . -iname "*.php" -exec php -l {} \; | grep -i "Errors.parsing"

MySQL: Find the size of a database

MySQL has the ability to show you size of the information contained in your database. You can find this information per database or per table in a database pretty easily.

Size of databases

To list the size of all the databases on the server run the following:

SELECT table_schema "Database", sum( data_length + index_length) / 1024 / 1024 
"Size in MB" FROM information_schema.TABLES GROUP BY table_schema;

Size of tables in database

To list the size of all the tables in an individual database run:

SELECT table_name, table_rows, data_length, index_length, 
round(((data_length + index_length) / 1024 / 1024),2) "Size in MB"
FROM information_schema.TABLES where table_schema = "{{NAME OF YOUR DATABASE}}";

Page 2 of 20