Git

How Can I Generate a Git Patch for a Specific Commit

Git users use the Git tool to manage and test the source code for DevOps projects. Developers modify the code, save their changes in a local repository, and after that, they push them to a remote repository. However, users occasionally want to send Git changes to another user for review before pushing them to a remote repository. In such a scenario, Git patch allows us to easily save changes in patch files and distribute them to other users.

This write-up will demonstrate the method to generate a Git patch for a specific commit.

How to Generate Patch for a Specific Git Commit?

Git patch is frequently used for sharing and reviewing Git commits in the form of a Git patch file. Users can generate the patch of commits using the below-listed methods:

Method 1: Generate a Git Patch of a Recent Commit

To generate a Git patch of a recent commit, first, open the Git repository. Then, use the “git format-patch -1” command to generate the patch file of a recent commit. For this purpose, follow the below-listed steps.

Step 1: Launch Git Terminal
First, launch the “Git Bash” Git terminal from the “Startup” menu:

Step 2: Go to Git Repository
Go to the Git repository through the “cd” command:

$ cd "C:\Git\Test"

Step 3: Make New File
Next, make a new file by using the “touch” command:

$ touch File1.txt

Step 4: Add Untracked Changes to Tracking area
Next, add the untracked file to the staging area by utilizing the provided command:

$ git add File1.txt

Next, check the Git repository state to view the tracked changes:

$ git status

Here, you can see the new file is successfully added to the tracking index:

Step 5: Commit Tracked Changes
Next, commit the tracked changes using the “git commit” command:

$ git commit -m "File1 committed"

After committing the changes, check the Git repository logs to verify whether the changes are committed or not:

$ git log

Step 6: Generate Patch of Recent Commit
Generate the patch of a recent commit using the “git format-patch” command. Here, the “-1” option is utilized to specify the number of commits included in the patch file:

$ git format-patch -1

Verify whether the patch file is generated by viewing the list of files and folders:

$ ls

The below output indicates that we have successfully generated a Git patch for a recent commit:

Method 2: Generate a Git Patch of a Specific Commit

To generate the patch of a specific commit, utilize the commit id along with the “$ git format-patch -1” command. Follow up on the provided steps to generate the patch file of a specific commit.

Step 1: Check Log
Firstly, check the Git log through the “git log” command. Then, write down the commit id of which you are required to create the patch file:

$ git log

Step 2: Generate Patch of Specific Commit
Next, utilize the “git format-patch” command along with Git commit id to generate the patch of the specific commit:

$ git format-patch -1 6c8694b

The output indicates that the patch of specific committed is generated:

You have learned how to generate the patch of Git commits.

Conclusion

Users of Git can create patches for both recent and specific commits. To generate a patch for the latest Git commit, utilize the Git “git format” command. However, run the “git format-patch -1 <commit id>” to generate a patch for a specific commit. This write-up has elaborated on the methods for generating patches of Git commits.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.