Search code examples
emacspatchmagit

How to create patch for staged files with Magit


How could I create patch for staged files with Magit?

I tried "W p", when prompt "Format range or commit(master)", I am not sure what should I put there, I tried HEAD..master, but it doesn't create any patch.

I tried command line: "git diff --cached > my.patch" create what I expected.

How could I create the same patch with Magit?


Solution

  • Magit uses git format-patch (see which for more information) to create patches1, and consequently requires the changes in question to have been committed.

    So commit your staged changes, then create your patch based on HEAD.

    Note that the metadata for the commit will be used to populate the patch headers.

    If you don't want to keep the commit, then simply soft-reset back to HEAD^ (you can type x at that previous commit).

    1 Note that this patch format is different to the plain git diff output, and must be applied with git am rather than git apply. At present magit only supports patch files in the format-patch format.

    You can still run arbitrary git commands without leaving magit, if you need to create or apply an unformatted patch file. Type ! for the appropriate menu.