Create a Git Patch From the Uncommitted Changes in the Current Working Directory

Better Stack Team
Updated on August 12, 2024

Creating a Git patch from uncommitted changes in your working directory can be very useful for sharing changes, applying them elsewhere, or for record-keeping purposes. Here’s how you can create a patch from your current uncommitted changes:

Creating a Git Patch From Uncommitted Changes

  1. Create a Patch File for Unstaged Changes

    To create a patch for changes that have been made but not yet staged for commit (i.e., untracked changes in your working directory), use:

     
    git diff > uncommitted-changes.patch
    
 
- `git diff`: Shows changes between your working directory and the index (staged area).
- `> uncommitted-changes.patch`: Redirects the output to a file named `uncommitted-changes.patch`.
  1. Create a Patch File for Staged Changes

    If you only want to create a patch for changes that are staged (i.e., added to the index but not yet committed), use:

     
    git diff --cached > staged-changes.patch
    
 
- `-cached`: Shows changes between the index and the last commit.
- `> staged-changes.patch`: Redirects the output to a file named `staged-changes.patch`.
  1. Create a Patch File for All Uncommitted Changes

    To create a patch that includes both staged and unstaged changes, you can combine the two outputs:

     
    git diff > all-changes.patch
    git diff --cached >> all-changes.patch
    
 
- `>>`: Appends the output to the file `all-changes.patch`.
  1. Apply the Patch (Optional)

    If you need to apply the patch later or on another branch, use the following command:

     
    git apply <patch-file>
    

    Replace <patch-file> with the name of your patch file, such as uncommitted-changes.patch.

Example Workflow

  1. Create Patch for Unstaged Changes

     
    git diff > uncommitted-changes.patch
    
  2. Create Patch for Staged Changes

     
    git diff --cached > staged-changes.patch
    
  3. Combine Both into One Patch File (if needed)

     
    git diff > all-changes.patch
    git diff --cached >> all-changes.patch
    
  4. Verify the Patch

    You can view the content of the patch file using a text editor or a command like cat:

     
    cat all-changes.patch
    
  5. Apply the Patch (if needed)

    To apply the patch later:

     
    git apply all-changes.patch
    

Summary

To create a Git patch from uncommitted changes:

  1. For unstaged changes: Use git diff > patch-file.patch.
  2. For staged changes: Use git diff --cached > patch-file.patch.
  3. To include both: Use git diff > patch-file.patch followed by git diff --cached >> patch-file.patch.

These patches can be applied using git apply <patch-file>, allowing you to transfer changes or keep a record of your work.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.