Can git automatically split up a commit into single hunks?

As explained in previous questions, you can split up commits into smaller patches. However, these answers recommend git add -p, which does the job but is tedious if all I want is to have one commit per hunk, in the given order in the file. Is there some way to achieve just that, automatically?

  • How can I set up autocompletion for Git commands?
  • .bashrc is not being read by git bash for git 2.5.1
  • How do I use git-new-workdir on windows?
  • Upgrading Git Bash to run newly downloaded ruby 2.0.0
  • How to open all modified files with git?
  • git: programmatically test if index is dirty
  • How to git blame a directory
  • Trigger build only on pushes to master
  • Using git as a better SVN
  • TeamCity The repository X has a submodule in the commit 'X' at a path 'X', but has no entry for this path in .gitmodules configuratio
  • Is it wise to use branching with one-man git project?
  • Building PhantomJS-2 from source on Windows
  • One Solution collect form web for “Can git automatically split up a commit into single hunks?”

    You could do something like

    echo -e "y\nq" | git add -p && git commit -m "automated"

    which does:

    echo y (accept first hunk), then q (quit) for the next hunk, commit with the given message.

    Loop until git commit does not return success:

    while [ $s -eq 0 ]
      echo -e "y\nq" | git add -p && git commit -m "automated $i"
      let i=$i+1

    or in one line:

    s=0; i=0; while [ $s -eq 0 ]; do echo -e "y\nq" | git add -p && git commit -m "automated $i"; s=$?; let i=$i+1; done

    Produces commits like

    c5ba3 - (HEAD) automated 3
    14da0 - automated 2
    6497b - automated 1
    Git Baby is a git and github fan, let's start git clone.