Git

How to Apply a Patch Generated With git format-patch

In Git, the different developers work together on the same project; therefore, it is frequently required to share project changes with each other or apply changes in another branch or repository. Git users can use the Git patch for this purpose, which allows them to save the changes in a file and later apply them to other repositories.

This post will demonstrate the method to apply the Git patch generated with the git format-patch command.

How to Apply a Patch Generated With the “git format-patch” Command?

To apply a generated patch, first, commit the changes. Next, generate the patch using the “git format-patch -1” command. After that, open the repository where you want to apply the generated patch and utilize the “git am <patch-path>” command.

Now, check out the below-given practical implementation.

Step 1: Launch Git Bash Terminal

First, open the Start menu and launch the “Git Bash” terminal:

Step 2: Go to Git Working Repository

Utilize the “cd” command and open the Git working repository:

$ cd "C:\Git\features"

Step 3: Generate New File

Generate the new text file using the “touch” command:

$ touch Demo.txt

Step 4: Add File to Staging Index

Move the untracked file to the tracking index to commit changes using the “git add” command:

$ git add .

Check the repository state to confirm if the file is added to the tracking index or not:

$ git status

Step 5: Commit Tracked Changes

Commit the tracked changes to add them to the local repository through the provided command:

$ git commit -m "New Changes are committed"

Check the Git log to verify if the changes are committed or not:

$ git log

Here, you can see new changes have been committed successfully:

Step 6: Generate a Patch

Generate the patch of committed changes using the “git format-patch” command:

$ git format-patch -1

It can be observed that the patch “0001-New-Changes-are-committed.patch” is generated:

Step 7: Apply Patch

Now, apply the patch in the same repository using the “git am <patch-file>” command:

$ git am 0001-New-Changes-are-committed.patch

Note: Do not worry if an error occurs if you are applying a patch to the same repository where you developed the patch and that repository has already committed modifications.

To handle this situation, skip the patch and apply it to another repository:

Step 8: Skip the Patch

To skip the applied patch, utilize the below command with the “–skip” option:

$ git am --skip

Step 9: Create New Repository

Make a new directory/repository, where we will apply the generated patch:

$ mkdir Demo

Next, use the “cd” command to navigate to the new repository:

$ cd Demo

Step 10: Apply a Patch on Another Repository

Lastly, apply the patch to the newly created repository using the “git am <“patch path”>” command:

$ git am "C:\Git\features\0001-New-Changes-are-committed.patch"

Here, you can see we have successfully applied the patch generated with the “git format-patch” command:

We have learned how to apply a patch generated with the “git format-patch” command.

Conclusion

To apply a patch generated with a “git format-patch”, first, go to the Git working repository. Then, commit the tracked changes using the “git commit” command. Next, generate a new Git patch using the “$ git format-patch -1” command. After that, go to a repository where you want to apply the generated patch and apply the patch using the “git am <“patch-path”>” command. This post has demonstrated the method for applying a patch generated with the git format-patch command.

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.