Don't stage or commit a file that contains certain text

to don something
don synonym
don in english
don urban dictionary
don clothing
don meaning slang
don medical abbreviation
don meaning italian

Is there a way to globally omit files from being staged or commited if they contain specific text?

Globally meaning independent of workspace/cwd

For example:

//TODO: test performance
var numbers = new List<int> { args };
decimal average = numbers.Aggregate(
    seed: 0,
    func: (result, item) => result + item,
    resultSelector: result => (decimal)result / numbers.Count);

The keyword here, '@NOCHECKIN', omitting the file from staging.

I would love to automate this by doing a simple git add . and not worry about it. Currently I have been using git add -i and stepping through

+visual-studio-code because I 99% of the time use the integrated terminal, but I am open to any answer, like a script (powershell or otherwise) or extension

Re globally: As pointed out this may have to be done per repository, I personally don't know, but I found a work-around that suits my needs. I just created a template dir with the pre-commit hook; now anytime I clone or do git init the hook is automatically included with the repos.

No, there is no way to do this. Git assumes that you know what you're doing when you stage a file and doesn't second guess you.

While it isn't possible to prevent a file from being staged, it is possible to use a pre-commit hook that uses git diff-index HEAD to find the files that have changed since the last commit and abort the commit if the file contains an appropriate entry. You'd need to do this on a per-repository basis unless you set core.hooksPath either globally or for the repositories you care about to use a shared hooks directory.

Don, Definition of don. (Entry 1 of 4) transitive verb. 1 : to put on (an article of clothing) donned his hat and gloves. 2 : to wrap oneself in : take on sense 3a the donning of new and more tyrannous moralities— Edward Sapir. To don means to put on, as in clothing or hats. A hunter will don his camouflage clothes when he goes hunting.

In pre-commit, you can reset a staged file from the index if the file contains the keyword, so that the file won't be committed. A sample pre-commit could be like:


# get the staged files
s_files=$(git diff --name-only --cached)

# if a staged file contains the keyword, get it out of the staged list
for s_file in ${s_files};do
    if grep -q -E '@NOCHECKIN' ${s_file};then
        echo "WARNING: ${s_file} contains the keyword"
        git reset ${s_file}

# if there is not any staged file left, fail the commit, otherwise
# an empty commit would be created.
s_files=$(git diff --name-only --cached)
if [[ "${s_files}" = "" ]];then
    echo "WARNING: nothing to commit"
    exit 1
exit 0

Don, DCP DeSilva (Boman Irani) sees a way to bring to justice the feared head of a criminal empire by recruiting a man named Vijay, who looks exactly like the crime boss (Shah Rukh Khan). The ruse works too well, and soon Vijay finds his life in danger Song - DON Singer- Gulzaar Chhaniwala Lyrics- Gulzaar Chhaniwala Music- Gulzaar Chhaniwala Mixing Mastering- Gulzaar Chhaniwala Dop/Edit/DI- Shiva Jangra

This is not really adequate, but: a clean filter could notice the //@NOCHECKIN and replace the entire file contents with an empty string, or just the //@NOCHECKIN. You could then (and/or also) test for such files in pre-commit and pre-push hooks as well, as bk2204 suggests.

It probably makes more sense to have such a clean filter generate a warning. That would be your notice that you should git rm --cached the file and then list it in a .gitignore.

You can also mark a clean filter as "required". I have never tested this to see how it works in practice, nor looked closely enough at the source to predict how it would work in theory.

See the gitattributes documentation for details on setting up a clean filter.

Don, verb (used with object), donned, don�ning. to put on or dress in: to don one's clothes. Origin of� The latest disease outbreaks around the world notified to the World Health Organization.

Don, A don is a guy that everyone wants to be like. He is not only sexy and muscular but amazing in bed aswell. He can rock anyone. Being a don is a talent that not� for requests or questions regarding SECNAV directives; and. for requests or questions regarding OPNAV directives. Thank you. SECNAV Directives Control Office DON/AA Directives and Records Management Division (DRMD) 1000 Navy Pentagon Room 5E170 Washington, DC 20350-1000. COMM: (703

don, 'She imagined a Spanish don living here in the 1800s, and building a stately hacienda in stages as his family grew.' More example sentences. 'The Perdido Star� Song Don't (feat. Zi) Artist Jerrod; Licensed to YouTube by SME, TuneCore (on behalf of Jerrod); Sony ATV Publishing, EMI Music Publishing, UNIAO BRASILEIRA DE EDITORAS DE MUSICA - UBEM, AMRA

don, To don means to put on, as in clothing or hats. A hunter will don his camouflage clothes when he goes hunting. We would like to show you a description here but the site won’t allow us.

  • Well git is right, I do know that I don't want to stage it :P You can do this with SVN scripts just seeing if there are ways to emulate with git CLI or VSCode, but I think their hook scripts work the same? Let me review some reading material on the hooks and if that isn't sufficient I'll post a bounty for any of the thinking out-of-the-box answers. Thanks a lot for the swift reply
  • @soulshined There is no staging area in SVN, so this would be impossible there anyways. The equivalent of what you could do with SVN would be the pre-commit hook as OP mentioned.
  • After reading up on it, this fits perfectly; even figured a way to go this 'globally'. Thank you very much bk!
  • I haven't thoroughly tested but at a high level this is exactly what I was looking for. Thank you!
  • After using this past month or two, this has worked very well in my daily workflow, so thank you. It even acts accordingly with --force (well at least what I expect to happen with force). Hasn't been any drawbacks to date personally
  • One note: you should probably use git grep -q -E @NOCHECKIN :$s_file (using git's grep so it can access the staged content, and adding the leading colon so it gets the staged content's id from the index and reads that rather whatever's at that path in the work tree at the moment).