Skip to content

Instantly share code, notes, and snippets.

@zlatko-minev
Last active November 10, 2020 00:49
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 zlatko-minev/2629929b8514780b71dba850503dacac to your computer and use it in GitHub Desktop.
Save zlatko-minev/2629929b8514780b71dba850503dacac to your computer and use it in GitHub Desktop.
Splitting a Git commit into one commit per file
#!/bin/bash
# Pretty much script from
# https://hisham.hm/2019/02/12/splitting-a-git-commit-into-one-commit-per-file/
# but modified to work with files that have spaces in their names
message="$(git log --pretty=format:'%s' -n1)"
git status --porcelain --untracked-files=no | while read status file
do
printf "\n"
echo "${status} ${file}"
filenm="$(echo "$file" | tr -d '"')" # strip " and add '
if [ "$status" = "M" ]
then
git add "${filenm}"
git commit -n "${filenm}" -m "update of $file: $message"
elif [ "$status" = "A" ]
then
echo " -> File added. Not doing anything right now."
# git add "${filenm}"
# git commit -n "${filenm}" -m "added $file: $message"
elif [ "$status" = "D" ]
then
echo " -> File removed. Not doing anything right now."
# git rm "${filenm}"
# git commit -n "${filenm}" -m "removed $file: $message"
else
echo " -> WARNING: unknown status $file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment