2

How we can un-commit a file if there is white space change expect general change

I follow the link Commit without whitespace changes on github

Community
  • 1
  • 1

1 Answers1

3

If you already committed in your local repo, you can remove whitepaces in files of your last commit with:

git rebase --whitespace=fix HEAD~

(as mentioned in "git remove trailing whitespace in new files before commit")

Note that if you already pushed the bad commit on GitHub, you would need a git push --force to publish the fixed commit: that can be problematic if others have already fetched from that same GitHub repo.


If you don't want to apply that fix to all files of a commit, but only to a specific file, before commit, you can create a patch based on its git diff, and apply that patch with the git apply --whitespace=fix option.

See "git-fix-whitespace" by Bruno Bronosky.


Update Oct. 2020 (five years later): you now have an alternative on GitHub side, using GitHub Action.
See "How to avoid whitespace being committed with GitHub?".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • like we have use git diff which show difference and what will we for white space if there is white space then file doesn't list else it list – smart developer Feb 20 '15 at 07:32
  • @smartdeveloper if git diff shows a difference, then you can apply the git-fix-whitespace" I mentioned to fix it. Then add and commit. – VonC Feb 20 '15 at 07:33
  • git -fix -whitespace Unknown option: -fix usage: git [--version] [--help] [-C ] [-c name=value] [--exec-path[=]] [--html-path] [--man-path] [--info-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=] [--work-tree=] [--namespace=] [] – smart developer Feb 20 '15 at 07:43
  • @smartdeveloper the way you would use that script (https://raw.githubusercontent.com/RichardBronosky/git-fix-whitespace/master/git-fix-whitespace) is by copying it in a folder referenced by your PATH, then add your files to the index (with their whitespace issue), and call the script with `git fix-whitespace --cached` (note the space between `git` and `fix-whitespace`) – VonC Feb 20 '15 at 07:49
  • abc@abc:/var/www/zends$ git fix-whitespace --cached git: 'fix-whitespace' is not a git command. See 'git --help'. pankaj@kindlebit-desktop:/var/www/zends$ – smart developer Feb 20 '15 at 08:28
  • @smartdeveloper that meant you didn't copy the file `git-fix-whitespace` in a folder referenced by your `$PATH`, or didn't make it executable (`chmod 755 git-fix-whitespace`. – VonC Feb 20 '15 at 08:30
  • I try this - > git rebase --whitespace=fix HEAD~ Here is message First, rewinding head to replay your work on top of it... Applying: white space – smart developer Feb 20 '15 at 11:55