| by Arround The Web | No comments

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

Git Patches are one of the most important functions of a Git application that are frequently used to implement and share the changes from one Git directory or repository to another Directory or repository. It is also used to share the committed and uncommitted changes with other developers for reviewing purposes. Moreover, both “git format-patch” and “git diff” commands are used for creating a patch.

This tutorial will demonstrate how to create a patch from the Git uncommitted changes.

How to Create a Patch From the Uncommitted Changes in Git Working Repository?

To create a patch from uncommitted changes in the Git repository, first, go to the repository, add changes to the staging index and utilize the “git diff –cached > Filename.patch” command.

For a practical guideline, go through the provided procedure.

Step 1: Launch Git Terminal
Launch the Git terminal from the Windows “Startup” menu:

Step 2: Go to Git Working Directory
Go to the Git working directory by utilizing the “cd <directory path>” command:

cd "C:\Git\Demo"

Step 3: Initialize Git Directory
Initialize the Git directory through the provided command:

$ git init

Step 4: Generate New File
Generate the new file by executing the “touch <File-name>” command:

$ touch File2.txt

Step 5: Add Untracked Changes
Next, move the untracked changes to the tracking index through the mentioned command:

$ git add .

Check the Git status to verify if the changes are added to the staging index or not:

$ git status

Here, you can see we have successfully added the untracked changes to the staging area:

Step 6: Generate Patch of Uncommitted Changes
In the next step, create the patch of uncommitted staged changes:

$ git diff --cached > Patchfile.patch

In the above command, the “–cached” option is used to create the patch of staged changes. If a user cannot use the “–cached” option, a patch of untracked changes will be created:

Use the “ls” command to view all directories and files of the current repository:

$ ls

Step 7: Apply Patch
Apply the patch in the same directory to check patch file is working or not:

$ git apply Patchfile.patch

It can be observed that an error is encountered because it already exists in the working directory:

Step 8: Make New Repository
Let’s make a new directory in which we will apply the recently created patch. For this purpose, utilize the “mkdir <directory-name>” command:

$ mkdir NewDirectory

After that, open the new directory or repository using the “cd” command:

$ cd NewDirectory/

Step 9: Apply Patch of Uncommitted Changes
Next, apply the patch into a new directory using the below-provided command:

$ git apply /c/Git/Demo/Patchfile.patch

To verify if the patch is applied or not, view the list of all files:

$ ls

The output indicates that we have successfully applied the patch of uncommitted changes into the new directory:

We have taught you the procedure to create a Git patch from the uncommitted changes.

Conclusion

To create a patch from the Git uncommitted changes, first, open the Git working repository. Create a new file and add it to the tracking index. After that, create a Git patch of tracked uncommitted changes using the “git diff –cached > Patchfile.patch” command. Next, apply the patch to another repository or directory through the “git apply <patch-path>” command. This post demonstrated the method to create a patch from the Git uncommitted changes.

Share Button

Source: linuxhint.com

Leave a Reply