Read more

Git blame: How to ignore white-space modifications

Arne Hartherz
December 19, 2012Software engineer at makandra GmbH

When doing a git blame, git will blame the person who added or removed white space in a line (e.g. by indenting), not the person who originally wrote the code.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Say git blame -w to ignore such white-space changes. You want this. \
Note that you can also use it when diffing: git diff -w.

Example

Consider this method, created by a user in commit d47bf443:

def hello
  'world'
end

^

$ git blame foo
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 1) def hello
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 2)   'world'
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 3) end

Now it's being changed to:

def hello
  if world?
    'world'
  else
    'universe'
  end
end

Here is how Git blames those lines with and without the -w switch:

$ git blame foo
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 1) def hello
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 2)   if world?
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 3)     'world'
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 4)   else
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 5)     'universe'
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 6)   end
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 7) end

^

$ git blame -w foo
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 1) def hello
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 2)   if world?
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 3)     'world'
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 4)   else
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 5)     'universe'
f5fae4c1 (Señor Foo     2012-12-19 14:47:36 +0100 6)   end
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 7) end
Posted by Arne Hartherz to makandra dev (2012-12-19 14:48)