'git apply' failed with code 1: trailing whitespace in SourceTree
Asked Answered
H

5

16

I'm using SourceTree for easy code reviewing before committing. Within SourceTree I often stage and commit only part of the lines which I changed in a document.

A couple days ago I received a piece of code from a programmer working on Windows, which caused some trouble with line endings. If I remember correctly, I then set core.autocrlf to true, which fixed the issues with the windows file. Since that moment however, I'm having major issues in another repo I'm working on. If I want to stage only part of a file in SourceTree I now get a massive error saying git apply' failed with code 1: [/a/long/path/here] trailing whitespace in SourceTree. So I figured I needed to set core.autocrlf back to what it was, which I "assumed" to be ("mother of all.. etc") false (I never messed with core.autocrlf before). So I set it to false which suddenly makes all files on which I change only one letter to be marked as having changed ALL lines in the file, which is obviously a major hassle during code review.

So I searched around and I found some examples of solutions here on SO, but that all uses adding extra parameters to git add and doing all kinds of weird stuff. Next to the fact that I don't think I can insert custom commands in the GUI of SourceTree I mostly wondered; How did it get so messy? And most importantly can't I just go back to how it was (whatever that was) instead of having to issue extra command everytime that I want to stage and commit?

Please note; I'm not afraid of the command line, I work in it pretty much the whole day. But I just like doing code reviews in a GUI such as SourceTree.

Any tip which helps me get things back to normal would greatly help me out!

Humdrum answered 10/9, 2015 at 16:27 Comment(0)
R
18

Set "Ignore whitespace". See screenshot:

enter image description here

Roer answered 13/2, 2018 at 14:57 Comment(4)
I would have preferred to just add this as a comment to @Mykola's answer, but it doesn't appear you can add images in comments :(.Roer
I was having OP's problem, but had already set Sourcetree's diff settings to "Ignore whitespace." Toggling to "show whitespace" fixed the issue for me.Treachery
this works on Sourcetree v4.1.5 (2021)Indorse
@LeeRichardson you can edit other peoples answers howeverAracelis
A
11

There is a discussion on Atlassian Community about this problem. The fix is to switch setting Show Whitespace to Ignore Whitespace.

Assam answered 5/2, 2018 at 15:15 Comment(0)
C
2

In your ~/.gitconfig, try adding the following section:

[apply]
    whitespace = nowarn

The full details of what this does are covered at https://git-scm.com/docs/git-apply

The options for apply.whitespace are (from the git-scm page):

  • "nowarn" turns off the trailing whitespace warning.
  • "warn" outputs warnings for a few such errors, but applies the patch as-is (default).
  • "fix" outputs warnings for a few such errors, and applies the patch after fixing them
  • "error" outputs warnings for a few such errors, and refuses to apply the patch.
  • "error-all" is similar to error but shows all errors.

Seems like the default for SourceTree under OS X is "error", at least with git 2.6.4. Or it may be interacting with our default of core.autocrlf = true

Culley answered 4/1, 2016 at 17:18 Comment(0)
A
2

Surprisingly, for me the solution was to do the exact opposite to the most upvoted answer, which was to switch from "Ignore Whitespace" to "Show Whitespace" as described in this answer.

Aracelis answered 10/3, 2023 at 9:23 Comment(0)
D
0

I believe this issue only affects the Mac version of SourceTree since I haven't ever encountered it on my Windows machines the past few years but can consistently reproduce it in macOS 12.5 with SourceTree 4.2.7 and embedded Git 2.41.0. Try switching from using the Embedded version of Git in SourceTree to using a system version (e.g., 2.39.3 from Homebrew).

Other answers on this page didn't work for me. Here's why:

  • The problem would appear when "Show whitespace" was enabled.
  • The problem would seem to not appear when "Ignore whitespace" was enabled except that I found this would cause SourceTree to inexplicably commit CRLF line endings even though Git is configured for core.autocrlf = input and I'm on a BSD system.
Desmoid answered 28/6 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.