Skip to content

Instantly share code, notes, and snippets.

@jdeniau
Created September 5, 2018 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdeniau/7dbdac3cf5d7c2fc91c0aa71980c6385 to your computer and use it in GitHub Desktop.
Save jdeniau/7dbdac3cf5d7c2fc91c0aa71980c6385 to your computer and use it in GitHub Desktop.
Split commit by file
git rebase --interactive --exec ./split.sh HEAD~1 # HEAD~1 can be replace by any commit number, this will split by commit + by file in this case
#!/bin/bash
set -e
LF=$'\n'
SHA=$(git rev-parse --short HEAD)
MSG=$(git show -s --format=%B HEAD)
set -f; IFS=$'\n'
FILES=($(git diff-tree --no-commit-id --name-only -r HEAD))
set +f; unset IFS
git reset HEAD^
for f in "${FILES[@]}"; do
git add "$f"
git commit -m "$SHA $f$LF$LF$MSG" --no-verify
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment