Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore one line in a specific file in Git

Tags:

git

I have a file database.properties which has

        password = something

So whenever I have to test something I will have to change this line to my local password. Everybody in the team will have their own local password.

I guess I will have to do one of the following :

  1. Every time I commit I should not add database.properties to index (I don't want to ignore this file), but when there are too many files it's easy to miss this file.

  2. Undo the changes before committing, but most of the time I forget to do this.

  3. Creating a profile may be a way to achieve this but even will have similar issues as database.properties.

I have checked Can git ignore a specific line? and How to tell git to ignore individual lines, i.e. gitignore for specific lines of code.

From temporarily ignoring files I see that we can do

git update-index --assume-unchanged <file>

but I will have to do this every time.

All the above references were asked/written before 3-4 years. So I just want to know If there is a better way to ignore just that one line.

EDIT

Following @VonC's answer I have tried to add a filter in .git/info/.gitattribute

{directory_path}/database.properties filter=password // I have given the correct directory path.

And I have added smudge and clean scripts as:

git config --global filter.password.smudge ~/smudge.sh 
git config --global filter.password.clean ~/clean.sh 

(When I run ~/smudge.sh and ~/clean.sh it replaces password= something line correctly. So the scripts are correct.)

But when I add/commit the database.properties, there seems to be no affect on database.properties file. (I am guessing ~/clean.sh should run when I add it to index)

What am I doing wrong?

like image 928
Karthik Avatar asked Aug 17 '15 08:08

Karthik


1 Answers

I stand by my 2014 answer in "Can git ignore a specific line?": a git content filter is made to change a line of a file by another content.

It still requires a (one time) git config command in order to declare that filter in your repo, but it will remain active (as oppose to a git update-index, which can be discarded on, for instance, a git reset)

The filter is associated to files in a .gitattributes file, put in the same folder as the database.properties. (see discussion)
It is associated to a program to execute in the local config of the repo.

like image 54
VonC Avatar answered Sep 29 '22 12:09

VonC