'how to rebase when prettier formatting was applied to whole project

Someone merged in prettier formatting changes across the whole codebase, and I am now trying to rebase against latest master/main/develop branch. How can I rebase most efficiently?



Solution 1:[1]

Starting point:

  • Checkout "old" hash, when prettier was updated: git checkout <prettier-was-updated>
  • Make sure that version is installed: rm -rf node_modules && npm install

Kickoff rebase:

  • git rebase --reapply-cherry-picks --empty=ask --reschedule-failed-exec --exec "npm run format:all" <older hash, about when prettier was updated>

This will go through trying to format each commit.

Rinse and repeat:

  • You'll be stopped at points with pure formatting changes. You can stash, re-run formatter, pop stash, and see there's no conflicts. (I'm assuming you aren't doing anything weird in-between, all your files are saved, etc)
  • Simply commit these changes as "squash: format previous commit"

Sometimes there will be merge conflicts:

  • Run git rebase --show-current-patch to see what the original commit was trying to do.
  • Typically, you'd look at this diff, and manually re-do what the original commit was trying to do.
  • However, this is wildly inefficient, these conflicts are purely due to formatting. Therefore, we can actually use some machinery to resolve the conflicts.
  • First run git rebase --show-current-patch. Copy the commit hash for this commit.
  • Run git show <paste sha you just copied>:src/path/to/CONFLICTED/file.ts
  • Copy the output (the file at this sha)
  • Paste into your editor/ide. You could stash the conflicted state, or stage it, etc.
  • Run formatter.
  • git rebase --continue

RINSE AND REPEAT!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1