How can I use git to stage only one line in a file for commit, all from a script?

Posted by Sandy on Stack Overflow See other posts from Stack Overflow or by Sandy
Published on 2011-01-06T18:38:32Z Indexed on 2011/01/06 18:54 UTC
Read the original article Hit count: 172

Filed under:
|

I'm writing a simple pre-commit git hook that updates the year in copyright headers for files that are staged for commit.

After modifying the line with the copyright, I would like the hook to stage that line so that it is part of the commit. It can't just git add the whole file, because there may be other pre-existing changes in there that shouldn't be staged.

I don't see any options in the git add manual the let you stage specific lines.

I figure I could git stash save --keep-index, apply my change, git add the file, and then git stash pop, but that seems rather crude. Any better approaches?

© Stack Overflow or respective owner

Related posts about git

Related posts about scripting