Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git ignore specific lines matching a pattern?

Tags:

git

I version control mysql database dumps from websites with git.

With --skip-extended-insert, so each record is on it's own line, it works quite well to track changes. It also allows me to pull old versions of the database from history and import them.

Is there a way to tell git to ignore certain lines or lines containing certain patterns?

like image 984
Jacob Dorman Avatar asked Sep 12 '12 06:09

Jacob Dorman


2 Answers

No

Git is file based, it will not modify the contents of tracked files to derive diffs.

Possible alternatives

Remove cruft

Don't include tables/data in the db dumps that you don't care for. If it's not there creating differences, you don't need to take extra steps to ignore/correct for it.

So if for example, the problem is deleted articles that you don't want to backup, remove them from the backup process:

mysqldump -c -w "articles.deleted IS NULL" articles > backup.sql 

Post process

Post process the database dump to remove things you don't care for. As an example, here's an extract from a db dump helper script I use:

#!/bin/bash
mysqldump -dRC --skip-dump-date --skip-add-drop-table --default-character-set=utf8 database $@ > schema.sql
sed -i 's/ AUTO_INCREMENT=[0-9]\+//' schema.sql

This example (for illustration only) removes autoincrement values from create table statements so that they don't generate differences in the (version controlled) schema.sql file.

like image 178
AD7six Avatar answered Oct 14 '22 12:10

AD7six


If git cannot do this, the two solutions I see are:

  1. Modify the script I use to dump the database to remove things that are irrelevant to version control. (drawback: removing some of these things might cause the dump to no longer be viable for import)

  2. Store each database dump somewhere else (maybe named by commitID somehow within a hook) and version control a modified version. e.g.,cat dump.sql | grep -v "_session" >> dump.sql, but it would be ideal if I could add a grep like this to git somewhere.

like image 28
Jacob Dorman Avatar answered Oct 14 '22 10:10

Jacob Dorman