Git Ignoring Files and Folders Ignoring only part of a file [stub]


Example

Sometimes you may want to have local changes in a file you don't want to commit or publish. Ideally local settings should be concentrated in a separate file that can be placed into .gitignore, but sometimes as a short-term solution it can be helpful to have something local in a checked-in file.

You can make Git "unsee" those lines using clean filter. They won't even show up in diffs.

Suppose here is snippet from file file1.c:

struct settings s;
s.host = "localhost";
s.port = 5653;
s.auth = 1;
s.port = 15653; // NOCOMMIT
s.debug = 1; // NOCOMMIT
s.auth = 0; // NOCOMMIT

You don't want to publish NOCOMMIT lines anywhere.

Create "nocommit" filter by adding this to Git config file like .git/config:

[filter "nocommit"]
    clean=grep -v NOCOMMIT

Add (or create) this to .git/info/attributes or .gitmodules:

file1.c filter=nocommit

And your NOCOMMIT lines are hidden from Git.

Caveats:

  • Using clean filter slows down processing of files, especially on Windows.
  • The ignored line may disappear from file when Git updates it. It can be counteracted with a smudge filter, but it is trickier.
  • Not tested on Windows