How to split all commits by file?

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

For example, commit Achanged 3 files, f1, f2 and f3. After the split, there are 3 fixations A-f1, A-f2 and A-f3.

I want to do this in order to facilitate a serious rewrite, since I only need to crush the small commits.

+4
source share
2 answers

Script

The following script splits a file HEADinto a file:

#!/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"
done

The generated commit messages have the form:

<original SHA> <file name>

<original commit message>

Using

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

, :

git rebase --interactive --exec git-split <branch>

, :

p Commit to split
x git-split

script .

+6

  • git diff-tree --no-commit-id --name-only -r <SHA1>
    
  • git show <SHA1>:/path/within/repo/to/file
    

, .

reset .

+2

Source: https://habr.com/ru/post/1661314/


All Articles