17 March 2025

There are two options when generating a patch from uncommitted changes: git diff and git diff --cached. git diff captures unstaged changes, modifications in the working directory that have not been added to the index. While git diff --cached captures staged changes that have been added to the index with git add but not yet committed. When creating a patch, git diff > file.patch will save only unstaged changes, whereas git diff --cached > file.patch will save only staged changes. Later, you can apply these patches using git apply file.patch to restore the changes in another working directory.

Source code viewer
  1. # Option 1: Create a patch from UNSTAGED changes (modifications not yet added with git add)
  2. git diff > unstaged_changes.patch
  3.  
  4. # Option 2: Create a patch from STAGED (CACHED) changes (modifications added with git add but not yet committed)
  5. git diff --cached > staged_changes.patch
  6. # OR
  7. git diff --staged > staged_changes.patch
Programming Language: Bash