Skip to content

Git patch workflow

The Git patch workflow allows developers to share, review, and apply code changes by generating patch files from specific commits rather than pushing branches directly^[600-developer__tools__git__git-patch-cherry-pick.md].

Generating patches

Patches are created using the git format-patch command, which translates commits into .patch files^[600-developer__tools__git__git-patch-cherry-pick.md]. This command offers several options to control the range of history included:

  • Single commit: To generate a patch for one specific revision, use the -1 flag^[600-developer__tools__git__git-patch-cherry-pick.md].
  • Multiple commits from HEAD: To create patches for the n most recent commits, use -n^[600-developer__tools__git__git-patch-cherry-pick.md].
  • Range of commits: To generate patches for all changes occurring between two specific commits (inclusive), use the three-dot syntax commitA...commitB^[600-developer__tools__git__git-patch-cherry-pick.md].
  • Since a specific commit: To include all commits made after a specific revision (excluding the revision itself), specify the commit hash alone^[600-developer__tools__git__git-patch-cherry-pick.md].

Applying patches

Once generated, patch files must be copied to the target Git repository^[600-developer__tools__git__git-patch-cherry-pick.md]. Before applying, it is standard practice to verify the integrity and compatibility of the patch:

  1. Check statistics: Use git apply --stat <filename> to inspect which files are modified by the patch^[600-developer__tools__git__git-patch-cherry-pick.md].
  2. Test application: Use git apply --check <filename> to ensure the patch can be applied cleanly without errors^[600-developer__tools__git__git-patch-cherry-pick.md].
  3. Apply: Use git am -s < <filename> to apply the patch and sign it off^[600-developer__tools__git__git-patch-cherry-pick.md].

Sources

  • 600-developer-tools-git-git-patch-cherry-pick.md
  • 600-developer__tools__git__git-patch-cherry-pick.md