UPGRADE TO GET UP TO 85% OFF GITKRAKEN PRO
Search
Close this search box.

Git Patch

Before Git pull requests existed, developers created Git patches to share their code with team members and project collaborators. Git patches are text files that contain code and Git commit metadata. Creating a Git patch is, in essence, copying and packaging up your work to send to someone else. Applying a Git patch involves taking someone’s work and adding it to your local Git repository

In this article, we’ll cover how to create and apply a Git patch using the CLI and GitKraken Desktop.

Quickly create and apply Git patches in GitKraken Desktop using the built-in CLI, command palette, or with a few clicks from the commit graph.

A Brief History of Git Patches

In the early stages of Git, developers shared their code by creating Git patches and sending them to their colleagues via email. At that time, Git pull requests didn’t exist like they do today, and Git patches were the best available option for sharing code. 

While it’s certainly more common now for developers to use pull requests to share code, there are still a few well-known projects that use Git patches. The Linux Kernel, Drupal, and Git projects frequently utilize Git patches. Projects that use Git patches today generally do so give project maintainers the ability to extensively review the changes in each Git patch before applying the patch, which adds another layer of security.  

Git Create Patch

To create a Git patch, you can run a variation of the git format-patch command depending on if you’re creating a patch for a single commit or from a Git branch. Once your Git patch has been created, you can send it, usually via email, to your team members for them to apply to their codebase

Git Apply Patch

To apply a Git patch, Git checkout the commit or branch where you want the changes applied, and then run the following command in the terminal:

git apply <.patch file>

While applying patches may take more time than other Git actions, the value is being able to add someone else’s work to your repo rather than having to write the code yourself. 

Git Patch Format 

The git format-patch command is used to create Git patches from the terminal. The basic syntax for this command looks like: 

git format-patch <commit SHA or branch name>

Visit Git’s official website for more information and variations of the Git patch format command

Git Create Patch in the CLI

Before you can create a Git patch from CLI, you’ll need to determine what file(s) you want to include in the patch. This will determine which variation of the git format-patch command you will use to create the Git patch.

Git Create Patch from a Single Commit in the CLI

To create a Git patch that contains information from a single Git commit, take the following steps:

  1. Type git log in the terminal and hit Enter to obtain the commit SHA of the commit you want to create a patch for. 
  2. Once you have the commit SHA, run: 

 git format-patch -1 <commit SHA> -o <name of the directory you want the patch saved to>

All of the changes included in the commit you selected will be packaged into a Git patch that you can then send to other collaborators.  

Git Create Patch from a Git Branch in the CLI

You can create a Git patch from a Git branch by performing the single step outlined below:

  1. Type git format-patch <branch name> -o <name of the directory you want the patch saved to> in to the terminal and hit Enter

This will create a Git patch that contains all of the information held on that Git branch including commits, diffs, new files, and any other changes.

How to Git Apply Patch in the CLI

To properly apply a Git patch in the terminal, you will need to perform the following steps:

  1. Git checkout the associated commit or branch you want the patch applied to
  2. Run the command: git apply <.patch file>

The changes contained in the patch file will then be reflected in your local repository. You can now move on with your workflow knowing that your local repository is up to date with other project collaborators. 

Git Create Patch with GitKraken Desktop

There are a few simple and intuitive ways to create a Git patch with GitKraken Desktop. Similar to creating a Git patch in the CLI, the way you create your Git patch depends on what type of files and how many you want to create a patch for. No matter how you create your Git patch, it’s easier with GitKraken Desktop, try it now for free.

Git Create Patch from a Single Commit with GitKraken Desktop

If you want to create and send a Git patch that contains information from a specific commit using GitKraken Desktop, take the following steps:

  1. From the central commit graph, right-click the commit you want to create the patch from
  2. Select Create patch from commit from the dropdown menu
  3. GitKraken Desktop will prompt you to save the patch to a specified location on your computer

Git Create Patch from Multiple Commits with GitKraken Desktop

To create a Git Patch that contains information from multiple commits, take the following steps:

  1. Hold Shift or Cmd to and select the commits you want to create the patch from
  2. Right-click one of the selected commits and select Create patch from commits from the dropdown menu
  3. GitKraken Desktop will prompt you to save the patch to a specified location on your computer

Git Create Patch from a Single Uncommitted File with GitKraken Desktop

To create a Git Patch that contains information from a single file that hasn’t been committed, take the following steps:

  1. From the commit panel located on the right side of GitKraken Desktop, right-click the uncommitted file you want to create the patch from
  2. Select Create patch from file changes from the dropdown menu
  3. GitKraken Desktop will prompt you to save the patch to a specified location on your computer

Git Create Patch from Multiple Uncommitted Files with GitKraken Desktop

In order to create a Git patch from multiple files that haven’t been committed, take the following steps:

  1. From the commit panel located on the right side of GitKraken Desktop, hold Shift or Cmd to select the uncommitted changes you want to create the patch from
  2. Right-click one of the uncommitted changes and select Create patch from changes in # files from the dropdown menu
  3. GitKraken Desktop will prompt you to save the patch to a specified location on your computer

Git Create Patch from all Working Directory Changes with the Command Palette

Follow the steps outlined below to use GitKraken Desktop’s Command Palette to create a Git patch from all your working directory changes!

  1. Access the Command Palette either by selecting the magic wand icon  🪄 or using the keyboard shortcut Cmd +  Shift + P
  2. Type patch into the Command Palette
  3. From the dropdown select Create patch from all working directory changes

Creating and applying Git patches isn’t a common workflow and can be intimidating when trying it for the first time. GitKraken Desktop makes working with Git patches simple, intuitive, and painless.

How to Git Apply Patch with GitKraken Desktop

GitKraken Desktop requires that patches be applied via the command palette. To apply a patch, perform the following actions:

  1. Git checkout the branch or commit you want to apply the patch to
  2. Access the Command Palette by selecting the magic wand icon  🪄 or by using the keyboard shortcut Cmd +  Shift + P
  3. Type patch into the Command Palette
  4. Select Apply patch; this will open your file explorer 
  5. From your file explorer, click the .patch file you want to apply and select Open

The Git patch will be applied to the branch or commit you have checked out and you can continue with your workflow.

Git to Patching!

Congratulations! Now you know how to create and apply Git patches using the CLI and GitKraken Desktop. This newly acquired knowledge opens the door for you to start contributing to open source projects and private repositories that use this unique Git workflow. 

Developers that know how to confidently use Git patches are pretty rare and incredibly helpful to have on any team. Combine your knowledge of Git patches with GitKraken Desktop to make creating and applying Git patches faster and easier! Download GitKraken Desktop for free today.

Additional Resources

Table of Contents

Make Git Easier, Safer &
More Powerful

with GitKraken

Visual Studio Code is required to install GitLens.

Don’t have Visual Studio Code? Get it now.

Team Collaboration Services

Secure cloud-backed services that span across all products in the DevEx platform to keep your workflows connected across projects, repos, and team members
Launchpad – All your PRs, issues, & tasks in one spot to kick off a focused, unblocked day. Code Suggest – Real code suggestions anywhere in your project, as simple as in Google Docs. Cloud Patches – Speed up PR reviews by enabling early collaboration on work-in-progress. Workspaces – Group & sync repos to simplify multi-repo actions, & get new devs coding faster. DORA Insights – Data-driven code insights to track & improve development velocity. Security & Admin – Easily set up SSO, manage access, & streamline IdP integrations.
winget install gitkraken.cli