Ignore one line in a specific file in Git

I have a database.properties file that has

  password = something 

Therefore, whenever I have to test something, I will have to change this line to my local password. Everyone in the team will have their own local password.

I think I will need to do one of the following:

  • Every time I commit, I should not add database.properties to the index (I do not want to ignore this file), but when there are too many files it is easy to skip this file.

  • Revert the changes before committing, but most of the time I forget for this.

  • Creating a profile may be a way to achieve this, but it will even have similar problems, such as database.properties .

I 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 what we can do

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

but I have to do it every time.

All the above links were asked / written up to 3-4 years. So I just want to know if there is a better way to ignore just one line.

EDIT

After @VonC's answer, I tried adding a filter to .git/info/.gitattribute

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

And I 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 correctly replaces the password= something . Therefore, the scripts are correct.)

But when I add / commit database.properties , the database.properties file does not seem to be affected. (I assume ~/clean.sh should run when I add it to the index)

What am I doing wrong?

+4
source share
2 answers

I support my 2014 answer in Can git ignore a specific line? :: A git content filter is created to modify the line of the file with other content.

The git config command (once) is still required to declare this filter in your repo, but it will remain active (as opposed to git update-index , which can be dropped, for example, git reset )

The filter is associated with files in the .gitattributes file placed in the same folder as database.properties . ( see discussion )
It is associated with a program for executing in a local repo configuration.

+1
source

A possible approach is to copy database.properties to database.properties.in and edit the latter to replace the password placeholder. Add database.properties (but not database.properties.in ) to your .gitignore file.

Then you will need to write a small script to copy database.properties.in to database.properties and enter the correct password. This script can be part of your build infrastructure (it will run after changing database.properties.in ), or you can configure it as a post-commit hook .

+1
source

All Articles