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?

  • cent os bash unrecognized option --system on adduser command
  • Installing git on shared hosting - ./configure: no such file or directory
  • Git .bash_profile return to default
  • git: changelog day by day
  • Git post-receive deployment stops working at random points
  • How to use bash variable correctly in gitconfig with alias
  • Cannot add to github: Gist does not support directories
  • Gitextensions with ssh and password
  • Change branch-off point
  • Using git with subversion
  • Install npm package from git/github - Module not found: Error: Can't resolve 'dom-helpers/class'
  • Can I use git to keep a remote server up to date?
  • 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.