How to apply Git Patches?

How to apply Git Patches?

Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository.

Jerry implements the strcat function for his project. Jerry can create a path of his code and send it to Tom. Then, he can apply the received patch to his code.

Jerry uses the Git format-patch command to create a patch for the latest commit. If you want to create a patch for a specific commit, then use COMMIT_ID with the format-patch command.

[jerry@CentOS project]$ pwd
/home/jerry/jerry_repo/project/src

[jerry@CentOS src]$ git status -s
M string_operations.c
?? string_operations

[jerry@CentOS src]$ git add string_operations.c

[jerry@CentOS src]$ git commit -m "Added my_strcat function"

[master b4c7f09] Added my_strcat function
1 files changed, 13 insertions(+), 0 deletions(-)

[jerry@CentOS src]$ git format-patch -1
0001-Added-my_strcat-function.patch

The above command creates .patch files inside the current working directory. Tom can use this patch to modify his files. Git provides two commands to apply patches git am and git apply, respectively. Git apply modifies the local files without creating commit, while git am modifies the file and creates commit as well.

To apply patch and create commit, use the following command in the branch you are intended to −

[tom@CentOS src]$ pwd
/home/tom/top_repo/project/src

[tom@CentOS src]$ git diff

[tom@CentOS src]$ git status –s

[tom@CentOS src]$ git apply 0001-Added-my_strcat-function.patch

[tom@CentOS src]$ git status -s
M string_operations.c
?? 0001-Added-my_strcat-function.patch

The patch gets applied successfully, now we can view the modifications by using the git diff command.

[tom@CentOS src]$ git diff

The above command will produce the following result −

diff --git a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..f282fcf 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,5 +1,16 @@
#include <stdio.h>
+char *my_strcat(char *t, char *s)
diff --git a/src/string_operations.c b/src/string_operations.c
index 8ab7f42..f282fcf 100644
--- a/src/string_operations.c
+++ b/src/string_operations.c
@@ -1,5 +1,16 @@
#include <stdio.h>
+char *my_strcat(char *t, char *s)
+
{
   +
   char *p = t;
   +
   +
   +
   while (*p)
   ++p;
   +
   while (*p++ = *s++)
   + ;
   + return t;
   +
}
+
size_t my_strlen(const char *s)
{
   const char *p = s;
   @@ -23,6 +34,7 @@ int main(void)
   {


Have more questions? Please email us at support@peerxp.com
Also, let us know if the article is helpful!

    • Related Articles

    • How To Change Git Remote URL?

      Introduction Git remote is a pointer that refers to another copy of the repository that is usually hosted on a remote server. In some situations, like when the remote repository is migrated to another host, you need to change the remote’s URL. This ...
    • How to duplicate a Git repository? Repository mirroring simplified.

      To duplicate a repository without forking it, you can run a special clone command, then mirror-push to the new repository. Before you can duplicate a repository and push to your new copy, or mirror, of the repository, you must create the new ...
    • Simple steps to synchronise a Remote Fork in a Git repository

      Introduction When you collaborate in any upstream project (like an open-source project) it is likely to be asked to make a fork of the main repository. A fork is a copy of the project in your Git account. This duplicate allows you to freely ...
    • PWSLab Git Workshop - Useful commands and concepts

      Agenda A brief history of Git. PWSLab walkthrough. Configure your environment. Workshop. Git Introduction Visit: https://git-scm.com/about Distributed version control. It does not rely on a connection to a central server. Many copies of the complete ...
    • Mirroring PWSLab Repositories to GitHub, Bitbucket and GitLab or any Git repositories

      Introduction Repository mirroring allows for mirroring of repositories to external sources. It can be used to mirror branches, tags, and commits between repositories. A repository mirror at PWSLab will be updated automatically. You can also manually ...