bash: ignore exit code, but retain output redirect

Try:

git diff --no-index -- ./a.json ./b.json > ./a-b.diff || true

Or :

git diff --no-index -- ./a.json ./b.json > ./a-b.diff || :

Or, as long as you aren't using bash -e:

git diff --no-index -- ./a.json ./b.json > ./a-b.diff
true

Basically, you can use any command after the diff that you know will produce a zero (success) exit code.


Note: the reason you have to use || true after a git diff --no-index is because you cannot ignore its exit code.
This has been made clearer with Git 2.25 (Q1 2020).

See commit 0115e5d (29 Oct 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit d4924ea, 01 Dec 2019)

git-diff.txt: document return code of --no-index

Signed-off-by: Denton Liu

Within diff_no_index(), we have the following:

revs->diffopt.flags.exit_with_status = 1;

...

/*
 * The return code for --no-index imitates diff(1):
 * 0 = no changes, 1 = changes, else error
 */
return diff_result_code(&revs->diffopt, 0);

Which means when git diff is run in --no-index mode, --exit-code is implied.
However, the documentation for this is missing in git diff.

Add a note about how --exit-code is implied in the --no-index documentation to cover this documentation blindspot.

Tags:

Bash

Diff