How to Create a Git Patch From Uncommitted Changes

John Wachira Feb 15, 2024
How to Create a Git Patch From Uncommitted Changes

This article illustrates how we can create a Git patch from the uncommitted changes in our working directory. This is handy when we want to create a patch without creating a commit.

Create a Git Patch From Uncommitted Changes

We will use an example to demonstrate how we can create a patch.

$ git status

git status

We can see that our working directory has some uncommitted changes. Running the git diff --cached command will output:

git diff –cached

To create a patch for this, we can run:

$ git diff --cached > mypatch.patch

We can add binary files with:

$ git diff --cached --binary > mypatch.patch

What if we have not staged the files for commit? Is the command the same?

To create a patch for unstaged changes, we can run:

$ git diff > mypatch.patch

In conclusion, we can create a patch for uncommitted changes in Git using the git diff command. This method allows us to create a patch without creating a commit.

You can apply the patch whenever you want to.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Patch