git

How can I make an individual commit message for each file (or even each part of a file) that has changed since the last commit


My preferred routine is to stage and commit my changed files all at once:

git commit -a
# enter commit message via editor

This works great when all of my changes are related to each other. However, when there are several unrelated changes (i.e. I forgot to make commits for each one), it makes for a lengthy commit message. In these cases, I'd rather have an individual commit message for each file, or even a separate commit message per change when there are multiple unrelated changes within a file. How can I do that?


Note: Difference between this and How do I commit only some files?

My question is different because it also addresses multiple commits within a file.


Solution

  • If you want to add a whole file, then use

    git add file.txt
    

    If you have separate changes within the same document that you want in separate commits, use a patch for selecting hunks

    git add -p
    

    When in interactive mode, if the hunks are too big, then you can split them by pressing S

    After splitting, if you find that the hunks still aren't granular enough, then see my answer about manually editing it to create precision patches.