Skip to content

Instantly share code, notes, and snippets.

@hello-josh
Last active April 23, 2024 20:37
Show Gist options
  • Save hello-josh/dbabb56e1a01ac293b83154a6f4a208e to your computer and use it in GitHub Desktop.
Save hello-josh/dbabb56e1a01ac293b83154a6f4a208e to your computer and use it in GitHub Desktop.
How to remove whitespace changes from a branch for a PR

How to remove whitespace-only changes from a PR

  1. Create a new local branch that will contain your changes without the whitespace

    git checkout -b tmp-branch

  2. Merge the changes that contain whitespace into your current branch WITHOUT committing the changes

    git merge --no-commit

  3. Create a diff that ignores whitespace and apply it to your branch. This creates a diff in unified format, ignoring whitespace, and not colorizing the output. Then using git to apply (like applying a patch) to the current directory.

    git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -

  4. Remove any remaining unstaged changes. The unstaged changes are the whitespace-only changes

    git checkout .

  5. Add any new files back to the staged changes since the diff/apply method ignores net-new files.

    git add .

  6. Verify your work

    git diff --cached

  7. Commit your work!

    git commit

@felix-manea
Copy link

felix-manea commented Jul 20, 2022

Thanks Josh for this tutorial!

This worked for me:

git checkout master
git checkout -b tmp-branch
git merge --no-commit [revision number]
git reset
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
git checkout .
git add .
git diff --cached
git commit

@hello-josh
Copy link
Author

You're welcome!

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