How to preserve git history when refactoring into multiple files

前端 未结 2 1354
礼貌的吻别
礼貌的吻别 2021-01-12 16:17

I have a SCSS file that is ugly. I want to split it up into multiple other files to make it easier to work with.

If my file is,

file.scss

相关标签:
2条回答
  • 2021-01-12 16:51

    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.

    0 讨论(0)
  • 2021-01-12 17:11

    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.

    0 讨论(0)
提交回复
热议问题