How to create and apply a Git patch file with git diff and git apply commands
Tags: git, beginners, programming, tutorial
Published: 2022-06-18 12:04:19

This article was originally posted on [Everything DevOps](https://everythingdevops.dev/how-to-create-and-apply-a-git-patch-with-git-diff-and-git-apply-commands/).

An engineer just joined their first company. While debugging an issue, their co-worker shared a **patch** (also known as **diff**) file to apply changes listed in the file to their local branch of the same Git repository.

The engineer could go line by line to retype and apply the changes, but doing so, will subject the code to human error. How can that engineer apply the changes easily?

In this article, you will learn:

- what a patch file is, 
- how the co-worker created the patch file with the `git diff` command, and 
- how the engineer can apply the changes using `git apply` command. 

## GitHub repository

This article will show screenshots of commands run on a cloned Git repository. Here is the [GitHub repository](https://github.com/Kikiodazie/demo-for-git-apply).

## What is a patch file?

A patch file is a file that contains changes to be applied to another file or files. A patch file records all the way files are different from each other. Patch files are sometimes called diff files and use `.patch` or `.diff` extension. 

## The `git diff` command and its use cases:

The `git diff` command works by outputting the changes between two sources inside a Git repository. The sources can be two different files, commits, branches, etc. 

To understand the above explanations better, below are common use cases of the `git diff` command.

**$ git diff**
When you run the `$ git diff` command, it outputs all the modified changes which are you yet to add to Git.

See the image of `$ git diff` in use below:

![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653760136708_Screenshot+2022-05-28+at+16.22.48.png)


**$ git diff <filename>**
When you run the `$ git diff <filename>` command, it will output the changes of the file (`<filename>`) to its previous committed state.

See image of `$ git diff <filename>` in use below:

![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653752352426_Screenshot+2022-05-28+at+16.39.03.png)


**$ git diff <branch_1_name> <branch_2_name>**
When you run `$ git diff <branch_1_name> <branch_2_name>` command, it will output the modifications between the two branches. 

See image of `$ git diff <branch_1_name> <branch_2_name>` in use below:


![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653752920244_Screenshot+2022-05-28+at+16.45.55.png)


Also, you can output the changes of your current branch to the mentioned branch to its previous committed state using just `$ git diff branch_name`. See the example image below:


![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653753148024_Screenshot+2022-05-28+at+16.52.18.png)


**$ git diff --staged path/to/file**
When you add changes to Git or move the change to staging, you will not be able to see the difference between the files. You can use the `git diff` command with **--staged or --cached** option to see the staged changes.


![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653753730068_Screenshot+2022-05-28+at+17.01.58.png)


**$ git diff commit_id1 commit_id2**
When you want to see the difference between any two commits, you can use the `$ git diff commit_id1 commit_id2` command.

To get the `id`'s of the commits you want to see the difference in, use the `$ git log` command to see the list of commits (starting from the latest commit) made in the Git repo and their respective commit ids.

![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653754572348_annotely_image+8.png)

## Creating a Git patch file with `git diff`

For the co-worker to create the Git patch file they shared with the engineer, they used any `git diff` commands you saw above to get the changes. And then save the changes to a patch file using `>`, as you can see below. 

```bash
$ git diff > patch_file.diff
```
Or
```bash
$ git diff commit_id1 commit_id2 > patch_file.diff
```
Or
```bash
$ git diff <filename> > patch_file.diff
```
, etc.

The above command will create the patch file in the current working directory, as seen in the image below.


![](https://paper-attachments.dropbox.com/s_356EDE695CC6E86B19A50AFF34AAAF4D9A2FA8BD602635ED1A481AA1E34D3F60_1653755976488_annotely_image+9.png)


Also, remember that the file can also have a `.patch` extension.

## Applying a Git patch file using `git apply`

After the created patch file has been shared with the engineer, they can apply it in the directory of the Git repository that requires the change using the command below.

```bash
$ git apply patch_file.diff
```
For more information about the `git apply`, check out [its man page](http://www.kernel.org/pub/software/scm/git/docs/git-apply.html).

## Conclusion

This article showed you how Git patch files are created using `git diff` and how the patch changes can be applied using `git apply`. 

There are other ways to apply changes from a patch file. To learn more about them, check out this conversation on [stackoverflow](https://stackoverflow.com/questions/12320863/how-do-you-take-a-git-diff-file-and-apply-it-to-a-local-branch-that-is-a-copy-o).