且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在提交并在git上载之前删除空白文件

更新时间:2022-11-15 21:10:02

如果您已经在本地存储库中提交过,则可以使用以下命令删除上次提交文件中的白点:

git rebase --whitespace=fix HEAD~

(如"在提交前git删除新文件中的尾随空白")

请注意,如果您已经 在GitHub上推送了错误的提交,则需要使用git push --force来发布固定的提交:如果其他人已经从同一GitHub上获取了该信息,则可能会出现问题回购.


如果您不想将该修订应用于提交的所有 个文件,而仅对提交的特定文件应用,则可以创建基于补丁的补丁程序.在其git diff上,并使用 git apply --whitespace=fix 选项应用该补丁.>

请参见" git-fix-whitespace "通过布鲁诺·布鲁诺斯基.


更新2020年10月(五年后):现在,您可以在GitHub端使用 GitHub Action .
请参阅"如何避免通过GitHub提交空白?".

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

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?".