English 中文(简体)
git - trim whitespace
原标题:

I ve accidentally put some whitespace in my initial commit - it shows up red in git diff --color. What s the best way to get rid of the existing whitespace and how can I avoid this happening again?

I am not necessarily looking for a built-in git command. Any external program available for free on Ubuntu would also be welcome.

最佳回答

To trim trailing whitespace on all files in the current directory, use:

sed -i  s/[[:space:]]*$//  *

To warn about future whitespace errors (both trailing spaces and spaces before tabs), and to fix whitespace errors in patches, add the following code to your gitconfig file:

[core]
    whitespace = trailing-space,space-before-tab
[apply]
    whitespace = fix
问题回答

core.whitespace instructs git to flag certain whitespace problems:

  • trailing-space warns about whitespace at the end of a line or at the end of a file
  • space-before-tab warns when there is a space before a tab used for indentation

apply.whitespace is used when applying a patch. It checks for whitespace errors (the ones listed above, in core.whitespace) and applies the patch after attempting to fix them (i.e., remove them).

These options go in ~/.gitconfig -- that is, a .gitconfig file at the root of your user s home directory (typically /home/user/.gitconfig on Linux, /Users/user/.gitconfig on Mac OS X, and I don t know where on Windows but I suppose somewhere in C:Documents and Settingsuser).

See this thread git remove trailing whitespace in new files before commit on using git rebase to strip whitespace from files that you ve already committed.

And to trim the white spaces from all files recursively from all sub directories this can be used.

find ./* -type f -exec sed -i  s/[[:space:]]*$//  {} ;




相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...

热门标签