How to preserve git history when refactoring into multiple files
You'll need to make one commit involving both the old and new file(s) for git to associate them in the future.
Make a copy of the original but don't change the new files just yet:
cp file.scss file-sm.scss
cp file.scss file-md.scss
cp file.scss file-lg.scss
Make a minor temporary change to the original to have it committed along with the new ones:
echo "// refactoring: split the file" >> file.scss
Commit the changes:
git add .
git commit -m "Refactor: splitting up file.scss"
Now you can see the history of the old file when you use --follow
and -C1
flags:
git log --follow file-lg.scss
git blame -C1 file-lg.scss
Clean up the "// refactoring .." comment and proceed with refactoring.
+1 for preserving history while refactoring - future developers will thank you.
Git has built-in rename / copy detection based on file similarity. To ensure it kicks in, first do a plain copy of file.scss
to all of file-{sm,md,lg}.scss
and commit these files. Then delete the unwanted portions from these files, remove file.scss
, and commit again. Now e.g. a git log --follow --find-copies file-sm.scss
should show file-sm.scss
's history including the one of file.scss
.