Search code examples
gitgit-rebasegit-rewrite-history

Split a single commit to multiple commits by filename without manually going through all files


I have a commit that has changes to 100 files. I want to split that commit to 100 commits with one commit by filename.

Currently I do git rebase -i <startpoint> then mark the commit I want to edit with e and reset HEAD^1 followed by series of git add && git commit -m <filename>. This becomes tedious with multiple files.

Preferably in one line of bash without scripts. Also it should be readable enough for memorization.


Update: Ended up using ElpieKay's solution with

git reset HEAD^ --soft
git diff --cached --name-only --relative | while read f; do git commit -m "$f" -- "$f"; done

Solution

  • Suppose it is the last commit and the commit id is abc123.

    Try

    git reset abc123 --hard
    git reset abc123^ --soft
    git diff --cached --name-only | while read f;do git commit -m "$f" -- "$f";done
    

    If anything goes wrong, run git reset abc123 --hard to restore.