Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to undo all whitespace changes with git

Tags:

git

I have a git repo, where I replaced a lot of files locally.

git status now shows many many modified files.

Some are "really modified", others only differ by line endings.

I want the ones that differ only by line endings to go away (git reset them), but I cannot seem to find the linux-piping-foo to make it happen.

Bonus points for how to remove files whose only difference is the executable bit.

like image 304
Lucas Meijer Avatar asked Oct 26 '10 20:10

Lucas Meijer


People also ask

How do I ignore whitespace changes in Github?

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

How do I undo a reset on github?

So, to undo the reset, run git reset HEAD@{1} (or git reset d27924e ). If, on the other hand, you've run some other commands since then that update HEAD, the commit you want won't be at the top of the list, and you'll need to search through the reflog .


2 Answers

This will do it:

  git diff -b --numstat \
| egrep $'^0\t0\t' \
| cut -d$'\t' -f3- \
| xargs git checkout HEAD --
  1. Run a diff of the working copy against the index and give a machine-readable summary for each file, ignoring changes in whitespace.
  2. Find the files that had no changes according to diff -b.
  3. Take their names.
  4. Pass them to git checkout against the branch tip.

This pipe will do something sensible for each step you leave off, so you can start off with the just the first line and add more to see what happens at each step.

A possibly useful alternative last line:

| git checkout-index --stdin

This would reset the files to their staged contents instead of to their last committed state.

You may also want to use git diff HEAD on the first line instead, to get a diff of the working copy against the last commit instead of against the index.


Note: if you have filenames with spaces in them, you will first need to add a tr:

  git diff -b --numstat \
| egrep $'^0\t0\t' \
| cut -d$'\t' -f3- \
| tr '\n' '\0' \

Then you must add a -0/-z switch to whichever final command you wanted to use:

| xargs -0 git checkout HEAD --
# or
| git checkout-index --stdin -z
like image 173
Aristotle Pagaltzis Avatar answered Oct 22 '22 06:10

Aristotle Pagaltzis


Though it seems you were originally looking for a pipe-based solution and you got it from @aristotle-pagaltzis, since it's a bit hard to remember, I think this alternative is worth noting:

git diff -b > gitdiffb
git stash  # or git reset --hard if you feel confident
git apply --ignore-space-change gitdiffb

If not just changes to whitespace numbers, but also whitespaces that are completely new or completely removed should be ignored, replace -b by -w.

The outcome differs from the pipe-based solution in the removal of whitespace changes even in files that also contain relevant changes. Thus, it's not exactly the way you described it, but for most people coming here via a search engine are probably rather looking for this.

like image 15
Yushin Washio Avatar answered Oct 22 '22 06:10

Yushin Washio