shekhardtu
shekhardtu

Reputation: 5380

How to ignore whitespace in github when comparing?

I have committed a file after prettifying it in sublime. Now when I am comparing the differences in GitHub web UI it's showing a lot of changes, so it is very difficult to see the actual changes. Can I compare files in there and ignore the whitespace?

Upvotes: 187

Views: 65688

Answers (6)

VonC
VonC

Reputation: 1330102

Since Oct. 2021, this option is now persistent!

Hiding whitespace is now remembered for each pull request

The diff setting to hide whitespace changes in the pull request Files Changed tab is now remembered for you for that pull request.

You no longer need to re-enable it when you switch to another tab or when you come back to the same pull request later.

https://i1.wp.com/user-images.githubusercontent.com/2503052/137387087-91cc8458-9e11-44a9-8da0-f48251ec452c.gif?ssl=1 -- hide-whitespace2

Learn more about reviewing proposed changes in a pull request.

As noted in "Allow users to set "Hide whitespace changes" by default #5486", this is not a satisfying solution:

  • This impacts every Go project, as Go's standard formatter (which pretty much everyone uses) enforces consistent whitespace.
  • For those who go through a lot of PRs, most of the time merge them without requesting changes, the per-PR setting is not nearly as helpful as a repo-level or user-level setting. Please add those.
  • I'm usually only opening each PR once, so this is completely useless.
  • I review a lot of PRs and only go there once, having to click navigate and make 3 clicks to 'hide whitespace' on each and every single PR is not solved by that 'partial addressing'.

That is why you have:


Since May 2018, this option is now more visible:

A diff view with reduced white space has been available since 2011 by adding ?w=1 to the URL.
The new button builds on this view to better support your existing workflows.

https://user-images.githubusercontent.com/602352/39442176-66700966-4c7e-11e8-91a9-a71db12eeac7.png

Select the new option "Hide whitespace changes" in that "Diff settings" new button, and you will see only the relevant changes you want.

Note: since 2018, that setting has been relocated to the setting "gear" symbol:

https://help.github.com/assets/images/help/pull_requests/diff-settings-menu.png


In discussions 65597, Robert Mikes suggests the following extension:

Another workaround is to use the Requestly browser extension to automatically add ?w=1 to the end of the relevant github.com URLs.

Here's my rule file that can be Imported into Requestly:

[
    {
        "creationDate": 1586890092492,
        "description": "Hide whitespace changes",
        "groupId": "",
        "id": "QueryParam_1586890092492",
        "isSample": false,
        "name": "Whitespace",
        "objectType": "rule",
        "pairs": [
            {
                "modifications": [
                    {
                        "actionWhenParamExists": "Overwrite",
                        "param": "w",
                        "type": "Add",
                        "value": "1"
                    }
                ],
                "source": {
                    "filters": {},
                    "key": "Url",
                    "operator": "Wildcard_Matches",
                    "value": "*github.com/*/files*"
                }
            },
            {
                "modifications": [
                    {
                        "actionWhenParamExists": "Overwrite",
                        "param": "w",
                        "type": "Add",
                        "value": "1"
                    }
                ],
                "source": {
                    "filters": {},
                    "key": "Url",
                    "operator": "Wildcard_Matches",
                    "value": "*github.com/*/files/*..*"
                }
            },{
                "modifications": [
                    {
                        "actionWhenParamExists": "Overwrite",
                        "param": "w",
                        "type": "Add",
                        "value": "1"
                    }
                ],
                "source": {
                    "filters": {},
                    "key": "Url",
                    "operator": "Wildcard_Matches",
                    "value": "*github.com/*/commits/*"
                }
            },
            {
                "modifications": [
                    {
                        "actionWhenParamExists": "Overwrite",
                        "param": "w",
                        "type": "Add",
                        "value": "1"
                    }
                ],
                "source": {
                    "filters": {},
                    "key": "Url",
                    "operator": "Wildcard_Matches",
                    "value": "*github.com/*/commit/*"
                }
            },
            {
                "modifications": [
                    {
                        "actionWhenParamExists": "Overwrite",
                        "param": "w",
                        "type": "Add",
                        "value": "1"
                    }
                ],
                "source": {
                    "filters": {},
                    "key": "Url",
                    "operator": "Wildcard_Matches",
                    "value": "*github.com/*/compare/*"
                }
            }
        ],
        "ruleType": "QueryParam",
        "status": "Active"
    }
]

Dec. 2024, from the same discussion, Yonatan proposes:

Another workaround to make it a one-click action. I wrote a tiny bookmarklet that simply adds the w=1 query param to the existing url and reloads the page.
Much faster than using the GitHub UI, although it still requires doing it for every PR, so obviously not ideal.

Save this as the url of a normal bookmark and name it whatever you like. When you're on a GH PR simply click the bookmark to apply:

javascript:(function()%7Bconst url %3D new URL(window.location.href)%3Burl.searchParams.set("w"%2C "1")%3Bwindow.location.href %3D url.toString()%7D)()

Upvotes: 81

DarkPlayer
DarkPlayer

Reputation: 389

Depending on the used prettifier or formatter, simply hiding whitespace changes may not be enough to get rid of all style changes. Many tools add line breaks or slightly adjust the code to match a specific code style. In this case, you need a semantic diff, a special diff tool that parses the code and filters out irrelevant changes.

There are a few options, such as difftastic or diffsitter, but they require you to check out the changes locally and run their tools in the terminal. The only tool I know of that works directly with GitHub is the one I am currently working on. So, disclaimer: The following part is about my own tool, SemanticDiff.

You can see SemanticDiff in action if you view the same reformat commit on GitHub and my diff viewer. The only changes that are still shown are whitespace changes inside doc strings as they technically modify the program. If you are you are working with public PRs/commits, you can try it out by simply pasting the corresponding GitHub URL at https://app.semanticdiff.com

Upvotes: 2

Neal Fultz
Neal Fultz

Reputation: 9696

Building on @Anthony and @Alan P's answer, here is a small bookmarklet for those that can't install chrome extensions:

javascript:document.location+="?w=1"

Upvotes: 1

Alan P.
Alan P.

Reputation: 3123

There is a Chrome plugin GitHub Whitespace by jackchuka that appends ?w=1 to the PR URLs so whitespace is always turned off

Upvotes: 3

Anthony
Anthony

Reputation: 15997

On github, you simply append the w=1 parameter to the URL for it to ignore whitespace.

That would look something like:

https://github.com/account_name/repo/pull/14/files?w=1

Upvotes: 271

Kristján
Kristján

Reputation: 18843

git diff -w (alternately git diff --ignore-all-space) ignores whitespace. You can also add --ignore-blank-lines if there are a lot of those.

For more details and many more options, check out the docs.

Upvotes: 9

Related Questions