How to split every commit by file?

前端 未结 2 1723
北海茫月
北海茫月 2021-01-06 03:52

I know how to manually split a commit using git rebase -i, but how can I automatically split every commit in a branch by file?

For instance, commit

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 04:29

    The Script

    The following script splits HEAD by file:

    #!/usr/bin/env bash
    set -e
    
    SHA=$(git rev-parse --short HEAD)
    
    # Change to repo root directory
    cd $(git rev-parse --show-toplevel)
    
    git reset HEAD^
    
    git diff-tree --no-commit-id --name-only -r $SHA | while read -r f; do
      git add "$f"
      GIT_EDITOR="echo '0a\n$SHA $f\n\n.\nw' | ed -s" git commit -c $SHA
    done
    

    The generated commit messages are of the form:

     
    
    
    

    Usage

    The following assumes that you can run above script as git-split.

    If you want to split all commits in a range by file, use it like this:

    git rebase --interactive --exec git-split 
    

    If you want to split a single commit during an interactive rebase, use it like this:

    p Commit to split
    x git-split
    

    Any improvements to the script are welcome.

提交回复
热议问题