diff --git a/plugin/magit.vim b/plugin/magit.vim
index 28cb489fe084c3ed9df12ace53bb0d9459e392fb..490d8e262c8d0256382756ffdcfc193a9b2a3c78 100644
--- a/plugin/magit.vim
+++ b/plugin/magit.vim
@@ -545,12 +545,17 @@ function! s:mg_git_apply(selection)
 	if ( selection[-1] !~ '^\s*$' )
 		let selection += [ '' ]
 	endif
-	silent let git_result=<SID>mg_system("git apply --cached -", selection)
+	" when passing List to system as input, there are some rare and
+	" difficultly reproductable cases failing because of whitespaces
+	let tmp=tempname()
+	call writefile(selection, tmp)
+	silent let git_result=<SID>mg_system("git apply --cached - < " . tmp)
 	if ( v:shell_error != 0 )
 		echoerr "Git error: " . git_result
 		echoerr "Tried to aply this"
 		echoerr string(a:selection)
 	endif
+	call delete(tmp)
 endfunction
 
 " s:mg_git_unapply: helper function to unstage a selection
@@ -568,12 +573,17 @@ function! s:mg_git_unapply(selection, mode)
 	if ( selection[-1] !~ '^\s*$' )
 		let selection += [ '' ]
 	endif
-	silent let git_result=<SID>mg_system("git apply " . cached_flag . " --reverse - ", selection)
+	" when passing List to system as input, there are some rare and
+	" difficultly reproductable cases failing because of whitespaces
+	let tmp=tempname()
+	call writefile(selection, tmp)
+	silent let git_result=<SID>mg_system("git apply " . cached_flag . " --reverse - < " . tmp)
 	if ( v:shell_error != 0 )
 		echoerr "Git error: " . git_result
 		echoerr "Tried to unaply this"
 		echoerr string(a:selection)
 	endif
+	call delete(tmp)
 endfunction
 
 " s:mg_get_section: helper function to get the current section, according to