Create the last commit patch file with Git using format-patch and apply to another

Using git-format-patch exports commits as patch files. You can then send these to your colleagues to be applied on another branched or cloned repository of the project. These patch files can contain one or more commits and Git replays that commit when you apply it.

Creating the last commit patch file

  1. Using terminal, change your current directory to the root directory
    cd /root/directory
  2. Enter the following to export the last commit to a patch file
    git format-patch -1 HEAD --stdout > file-name.patch

Applying the patch file

  1. Using terminal, change to another root directory of a branched or cloned repository
    cd /other/root/directory
  2. Check the patch file if it would apply to the branched or cloned repository
    git apply --check file-name.patch
    If there are warnings or errors, you will have to resolve these issues first.
  3. If there are no warnings or errors, you can apply the patch
    git am --signoff < file-name.patch
  4. Lastly, push these changes to the branched or cloned repository
    git push



Thank you a lot, you saved my clean git history from an awful merge or to break the upstream.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.