The Wayback Machine - https://web.archive.org/web/20250116093740/https://www.geeksforgeeks.org/create-a-git-patch-from-uncommitted-changes/
Open In App

Create a Git Patch from Uncommitted Changes

Last Updated : 04 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Creating a Git patch from uncommitted changes is a handy way to share your code changes without committing them. It’s useful for code reviews, collaboration, and backups. A Git patch captures the differences between your working directory and the repository, and you can apply it later to another branch or repository. In this article, we’ll walk through how to create a Git patch from uncommitted changes in your current working directory.

What is a Git Patch?

A Git patch is a file that contains changes made to a codebase, including additions, deletions, and modifications. It can be applied to other branches or repositories to replicate the changes. Patches are typically used for:

  • Sharing changes with team members.
  • Submitting changes for code review.
  • Creating backups of your changes.
  • Applying changes to different branches or repositories.

Why Create a Git Patch from Uncommitted Changes?

  • Share Without Committing: Share your changes without committing them to the repository.
  • Review Changes: Send changes for review before committing.
  • Collaborate Efficiently: Collaborate with others who can apply the patch to their local repositories.
  • Backup Changes: Create a backup of your changes that can be applied later.

Method 1: Using git diff

The git diff command is used to generate a patch file that captures the differences between your working directory and the last commit. This method is suitable for creating patches from uncommitted changes, whether they are staged or unstaged.

Step 1: Make some changes to files in your repository.

Step 2: Stage specific changes using git add (Optional)

git add file1.txt file2.txt

Step 3: Generate the patch file

git diff > mypatch.patch

Step 4: Inspect the patch file to verify its contents

cat mypatch.patch

Step 5: Share the patch file or apply it using git apply

git apply mypatch.patch

Output:

Screenshot-2024-06-27-160758
Output using git diff

Note: The .patch file will be stored into the working folder.

Method 2: Using git format-patch

The git format-patch command is used to create a series of patch files from a range of commits. This method is generally used for committed changes, but it can be adapted for uncommitted changes by creating a temporary commit.

Step 1: Make some changes to files in your repository.

Step 2: Stage the changes you want to include in the patch

git add file1.txt file2.txt

Step 3: Identify the commit to diff against (replace HEAD with the desired commit hash if needed)

git log --oneline

Step 4: Generate the patch file

git diff --cached HEAD | git format-patch -1 --stdout > mypatch.patch

Step 5: Inspect the patch file to verify its contents

cat mypatch.patch

Step 6: Share the patch file or apply it using git 'am'

git am mypatch.patch

Output:

Screenshot-2024-06-27-161636
Output using git format-patch
Note: The .patch file will be stored into the working folder.

Conclusion

Creating Git patches from uncommitted changes is a valuable skill for collaboration and code management. By following these approaches, we can effectively share, review, and manage changes before committing them to our repository.

Become a Full-Stack Developer and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg