6

I am trying to move uncommited changes from a local git repository to another local repository. On repo 1 I create a patch like this:

git diff > my_patch.patch

Inspecting the patch:

more my_patch.patch

I get the same output as running git diff on the first repo. In the second repo I run:

git apply --stat my_patch.patch
0 files changed

If I run git apply my_patch.patch I get no effect.
Am I creating the patch wrong? Am I applying it wrong?

Steps to reproduce the issue:

  1. Clone a repository 2 times (same branch checkout)
  2. In first repo, make some changes to a file (uncommited).
  3. Create patch from changes: git diff > ~/my_patch.patch
  4. Go to second repository. Try and apply patch: git apply ~/my_patch.patch
  5. Run git status. No changes!

Suggesting another way of moving uncommited changes to another local repo would also be good to solve my issue.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It would help if you created a [mcve]. Note that `git apply --stat` tells you what *would* change, without applying, as it disables the `--apply` option. – torek Apr 29 '20 at 09:15
  • I know, I just wanted to show that git will not make any change from the .patch file. – Alexandru Rebega Apr 29 '20 at 10:25
  • 1
    If I try your 5 steps, the result is that `git status` shows me `Changes not staged for commit` in the modified file, and `git diff` in the clone in which the patch is applied shows the same patch I applied with `git apply`. – torek Apr 29 '20 at 10:38
  • I cannot reproduce the problem. The patch applies on the clone and I see the differences applied there. – michid Apr 29 '20 at 20:30

1 Answers1

4

Not only your usecase now (Q4 2021, Git 2.34+) is working as expected, but Git 2.35 (Q1 2022), "git apply"(man) has also been taught to ignore a message without a patch with the --allow-empty option.
And it learned to honor the --quiet option given from the command line.

See commit 324eb77, commit c21b8ae (13 Dec 2021) by Jerry Zhang (jerry-skydio).
(Merged by Junio C Hamano -- gitster -- in commit 62a3a27, 22 Dec 2021)

git-apply: add --allow-empty flag

Signed-off-by: Jerry Zhang

Some users or scripts will pipe "git diff"(man) output to git apply(man) when replaying diffs or commits.
In these cases, they will rely on the return value of "git apply" to know whether the diff was applied successfully.

However, for empty commits, "git apply" will fail.
This complicates scripts since they have to either buffer the diff and check its length, or run diff again with "exit-code", essentially doing the diff twice.

Add the "--allow-empty" flag to "git apply" which allows it to handle both empty diffs and empty commits created by git format-patch(man) --always by doing nothing and returning 0.

git apply now includes in its man page:

[--verbose | --quiet] [--unsafe-paths] [--allow-empty] [...]

git apply now includes in its man page:

--allow-empty

Don't return error for patches containing no diff.
This includes empty patches and patches with commit text only.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250