I came up with this semi-automatic approach:
I started with git rebase -i main
and replaced every pick
operation with an edit
operation. (Vim: :%s/pick/edit/
)
Instead of conflict resolving manually, I use git checkout REBASE_HEAD .
, to replace the working tree with the non-code formatted version and then run the code formatting tool again. (In this example ./vendor/bin/php-cs-fixer fix
)
Because the rebase behavior is slightly different if a conflict occurs, you need to follow up with different commands to complete the current commit based on the state:
This command if you encounter a normal "edit" breakpoint:
Looks like
Stopped at abc123... [Commit Message...]
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
After checkout and code format, amend current commit and continue rebase:
git commit --amend -a --no-edit && git rebase --continue
Complete one-step command:
git checkout REBASE_HEAD . && ./vendor/bin/php-cs-fixer fix && git commit --amend -a --no-edit && git rebase --continue
This command if you encounter a rebase conflict:
Looks like this (hints might be colored):
Auto-merging [File]
CONFLICT (content): Merge conflict in [File]
error: could not apply abc123... [Commit Message]
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply abc123... [Commit Message]
After checkout and code format, stage changes and let git rebase --continue
amend the commit:
git add -u . && GIT_EDITOR=true git rebase --continue
Complete one-step command:
git checkout REBASE_HEAD . && ./vendor/bin/php-cs-fixer fix && git add -u . && GIT_EDITOR=true git rebase --continue
If you use the wrong command, the end result will be the same, but you will lose some commits.
Sadly, I couldn't figure out a way to use git rebase --exec
(REBASE_HEAD
isn't defined during the exec command?) or a way to automatically use the correct resolve command.
I'm sure there is a better solution, but I couldn't find it, so I present mine here.