Git Patch Command

Learn via video courses
Topics Covered

Overview

The git patch is a very useful feature provided by Git which allows us to share our changes with our fellow developers without even pushing those changes to the master or main branch of the working repository. The git patch is also termed as git diff command. The command creates a file named git patch file which contains all the changes and we can share this git patch file for suggestions.

Pre-requisites

The prerequisites for learning the git patch command can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git rebase command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems namely - Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.

Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.

What is a Git Patch?

For understanding the git patch, let us take a scenario in which we are working on a project. As we know, we create branches for parallel development and bug fixing, so we created a branch and did some changes accordingly. Now, we can push those changes to the master or main branch. But wait there can be a situation in which our pushed changes have some issues which can hamper the working main branch or the production branch. Also, there can be a situation in which our fellow developers are suggesting some changes in our current pushed change. So, to deal with such a situation it is better to communicate about the changes with our team and then push them.

So, the git patch is a very useful feature provided by Git which allows us to share our changes with our fellow developers without even pushing those changes to the master or main branch of the working repository. In simpler terms, we can say that a git patch is another way of changing code changes with others.

The git patch is also termed as git diff and this awesome feature creates a file named git patch file which contains all the changes we can share this git patch file for suggestions. Now, after the approval of the git patch file by our fellow developers, we can push the changes to the master branch.

Refer to the image provided below for more clarity.

git patch introduction

Patches can be used in various ways such as:

  • bug fixing,
  • security vulnerability fixing.
  • styling fixing,
  • performance enhancement, etc.

So far we have discussed that we create a path file and send it to fellow developers but what is this git path file? Well, a git patch file contains code as well as the metadata about the commits such as the ID of the commit, the date and time of the commit, the commit message, etc. A git patch file is similar to the git diff (refer to the next section(s) for more details). A git patch can be created using the commit and our fellow developers can apply the patch to their repository. So, we can say that the git commits can be shared or exported using the git patch in the form of plain text files.

Creating a Git Patch with Git Diff

We can create the git patch file using a command called git diff. Before learning about how can we perform the git patching using the git diff command, let us first learn about the functions of the git diff command. When we want the changes or the difference between the two sources present in a git repository, we use the git diff command. The git diff command is used to get the output of the changes between two sources.

Some of the important functionalities of the git diff command are:

  • To get all the modified changes of the file(s) that are not added to the git or the file(s) that are not yet staged.
  • To get the changes of the current file concerning its previous committed version.
  • To get the modifications made in the current branch concerning its previous committed version.
  • To get the difference between two commits using the commit IDs of both the commits.

Now to create a git patch using the git diff command, we first need to grab the changes using the git diff command and then save the obtained changes into a .patch file of our choice. Please refer to the command provided below for more clarity.

Creating a Patch for a Single File

Suppose we have a single file named test.txt and we have made some changes to it. We can use the git diff test.txt command to check the changes made. We can also use the git diff --cached test.txt command after the file is staged.

Now, if we want to create the path for the single test.txt file that we have changed, we can use the git diff command. Let us see the whole command for the same.

In the above command, we are saving the patch in a file named test-patch-file.patch.

Creating a Patch for a Binary Image

Suppose we have some binary files in our working directory such as a jpg file or a png file. If we have made some changes in these binary files then we can see the changes using the command git diff -–staged –-binary > binary-file. In the command, we first stage the files using the --staged flag and then use the git diff command to see the changes in the previous file and the currently staged file.

Now after staging the file, we can create the patch file of the difference using the command:

The above command is quite the same as the single file command, here we are saving the patch in a file named binary-patch-file.patch.

Creating Patches from the Commits

In the previous section, we learned about patching single files and binary files. Let us now learn how we can create the patch file(s) for all the commits made in the current working branch. Suppose the name of the branch is test-branch and the number of commits that is to be included in the resultant patch file is 1.

The command to create the patch from the commit is:

Note: We can even use the SHA-value of the head and generate the patch file for the same. The command will be:

Now, if we want to create a patch from one commit to the other commit, we can include the starting and ending commit in the git diff command. Here, we need to mention the SHA-Value of the HEAD of both the commits. Let us now look at the overall command for the same.

In the above command, we are saving the patch in a file named test-patch-file.patch. The SHA-value of the starting commit is: 43b720b and the SHA-value of the ending commit is:8435ea9.

Note: Git manages the versions of the directory as well as handles the security using the cryptographic method SHA-1.

Apply the Git Patch

So far we have learned how we can create a patch file, let us now learn how we can apply the git patch. To understand the working, let us take a scenario. We have a patch file named test.txt and the changes are written in this file. Now if the user wants to get all the changes and check if the changes are correct or not, he/she can create a different branch from the main or master branch and then apply the patch to check the changes.

The command for the same is:

In the above command, the test-patch.patch is the name of the patch and it is applied using the git patch command.

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc.
  • The git patch is a very useful feature provided by Git which allows us to share our changes with our fellow developers without even pushing those changes to the master or main branch of the working repository.
  • The git diff command is used to get all the modified changes of the file(s) that are not added to the git or the file(s) that are not yet staged.
  • The git diff command is used to get the modifications made in the current branch concerning its previous committed version.
  • The git diff command is also used to get the difference between two commits using the commit IDs of both the commits.
  • The git patch is also termed as git diff command. It creates a file named git patch file which contains all the changes and we can share this git patch file for suggestions.
  • Patches can be used in various ways such as bug fixing, security vulnerability fixing, styling fixing, performance enhancement, etc.
  • The git commits can be shared or exported using git patch in the form of plain text files. A patch can be created using the commit and our fellow developers can apply the patch to their repository.
  • A git patch file contains code as well as the metadata about the commits such as the ID of the commit, the date and time of the commit, the commit message, etc.