Skip to content

How to fix merge conflicts related to the black code style reformatting of the scikit-learn code base #20301

Closed
@ogrisel

Description

In #18948 we recently started to use black to format almost all of scikit-learn's code, so it's possible that your PR is conflicting with the main branch due to the new code formatting style. Here are some instructions to resolve conflict that you may encounter when merging your PR's branch with the main branch.

Here we assume that upstream is an alias for the https://github.com/scikit-learn/scikit-learn git repo or its ssh variant. You can confirm that on your end by checking the output of git remote -v.

You could try to directly merge with main and then apply black locally, but this is likely to introduce a lot of conflicts due to the formatting style change. Instead, we recommend the following workflow:

  • Abort any previous merge attempts in your branch: git merge --abort. If you weren't in the middle of a merge before this will fail with "fatal: There is no merge to abort". It's normal, you can safely ignore this message.

  • Fetch all the commits from the main repo git fetch upstream

  • Merge your PR branch with the commit right before the black formatting commit:

    git merge 0e7761cdc4f244adb4803f1a97f0a9fe4b365a99
    

    This will import the black config and deal with existing merge conflicts before black formatting.

  • Cherry-pick the commit the updates the black config with target_version:

    git cherry-pick e8f58cddb55777e0e40195aaf64d5a890b9fdaac
    

Then, locally apply black to all the code:

  • pip install black==21.6b0

  • To format the code changes in your PR branch from the top level scikit-learn source folder:

    git diff $(git merge-base upstream/main HEAD) --name-only -- '*.py' | xargs black
    
  • Commit black formatted changes.

  git commit -am "apply black"
  • Merge your branch with main:
    git fetch upstream main
    git merge upstream/main
    

This should work without code-style related conflicts.

If git blame is important to you

For git blame to work locally run:

git config blame.ignoreRevsFile .git-blame-ignore-revs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions