title: Can you change a file content during git commit?
body: <p>One of the things I keep in my <a href="http://jj.github.io/hoborg/" rel="noreferrer">open novel in GitHub</a> is a <a href="https://github.com/JJ/hoborg/blob/master/text/words.dic" rel="noreferrer">list of words</a> I would like to set automatically the first line, which is the number of words in the dictionary. My first option is to write a pre-commit hook that reads the file, counts the words, rewrites the first line and writes it back again. Here's the code</p>
  
  <pre><code>PRE_COMMIT {
    my ($git) = @_;
    my $branch =  $git-&gt;command(qw/rev-parse --abbrev-ref HEAD/);
    say "Pre-commit hook in $branch";
    if ( $branch =~ /master/ ) {
       my $changed = $git-&gt;command(qw/show --name-status/);
       my @changed_files = ($changed =~ /\s\w\s+(\S+)/g);
       if ( $words ~~ @changed_files ) {
         my @words_content = read_file( $words );
         say "I have $#words_content words";
         $words_content[0] = "$#words_content\n";
         write_file( $words, @words_content );
       }
     }
  };
  </code></pre>
  
  <p>However, since the file has already been staged, I get this error</p>
  
  <blockquote>
    <p>error: Your local changes to the following files would be overwritten
    by checkout: 
      text/words.dic 
    Please, commit your changes or stash them
    before you can switch branches. 
    Aborting</p>
  </blockquote>
  
  <p>Might it be better to do it as a post-commit hook and have it changed for the next commit? Or do something completely different altogether? The general question is: if you want to process and change the contents of a file during commit, what's the proper way of doing it?</p>