How to use git patch effectively
Tags: git, beginners, programming, tutorial
Published: 2020-07-18 05:59:08

---
title: How to use git patch effectively
published: true
date: 2020-07-14 09:00:32 UTC
tags: git, beginners, programming, tutorial
canonical_url: https://www.srebalaji.com/how-to-use-git-patch-effectively/
---

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F5f01cf76-54b5-4843-b54b-381734b9de16_500x334.jpeg)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

Git provides the option to create patch files. These patch files contain changes that can be used elsewhere.

For example, you can create a patch file in your local repo. Then you can send it to others to use the patch in their local.

Some people will also create a patch to have it as a backup for later use.

#### Creating git patch

```
git format-patch <target-branch>
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fae46eada-3781-4d44-8449-aeb7f8ab9071_629x509.png)

In the above example, you can see that I have mentioned **master** as the target branch. So Git will fetch all commits from the current branch (develop) which are ahead of master.

Then it creates a patch file for each commit. As you can see there are eight patch files created.

##### Creating a single patch file for multiple commits

```
git format-patch <target-branch> --stdout patch_name.patch
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Ff752c0a0-c093-4c89-8f30-979e93344281_633x328.png)

In the above example, a single patch file is created with a given name for all the commits.

##### Creating a patch from git commits

Sometimes, you are interested in creating a patch from a selective commit. In this case, you can use

```
git format-patch -1 <commit-hash>
```

The above command will only consider the respective commit hash to make a patch.

#### Applying git patch

Now you know how to create a patch file. Let’s see how to apply the git patch if you got one.

```
git apply <patch_file_path>
```
![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F638fc90e-ed13-4984-a429-eff625703320_647x283.png)

You can see that I have applied a patch file from the destined path. Then the changes are applied from the patch file. Then you can review those changes and do the commit.

```
git apply --stat <patch_file_path>
```

This command will not apply the patch but will show the changes stat.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F1123dee8-fffa-41dc-9af9-43deab833505_693x150.png)

##### Using git am

```
git am <patch_file_path>
```

This command will apply the patch and a new commit is created from the changes. Unlike **apply** it won’t be just used to preview the changes. **git am** will create new commits.

Patches are helpful if you want to share code with others without using your centralized repo. And it is also useful to take a backup of your work for later purposes.

Thank you for reading


> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.