Using git diff as a filter
We will see how we can use git diff as a filter in this post.
Applying git diff on files
Let’s say you have a file and made some edits. You can use the following command. Remember that the filter name is a name that you choose and can be anything.
Normally, when you use git diff <file-name>, you get an output on the command line with differences highlighted. Let’s say I added a sentece to this post and used git diff. Here is the output:
diff --git a/_posts/2019-12-18-gitdiffasfilter.md b/_posts/2019-12-18-gitdiffasfilter.md
index 4a340cb..68cf6c6 100644
--- a/_posts/2019-12-18-gitdiffasfilter.md
+++ b/_posts/2019-12-18-gitdiffasfilter.md
@@ -3,9 +3,12 @@ layout: post
title: Using git diff as a filter
---
-We will see how we can use `git diff` as a filter in this post.
+We will see how we can use `git diff` as a filter in this post.
+
## Applying `git diff` on files
+Let's say you have a file and made some edits. You can use the following command. Remember that the `filter` name is a name that you choose and can be anything.
So, the following command will allow you to write the output to a text.
$ git diff file_original file_modified > filter
Make sure you are on your root directory level where your filter is located.
$ git apply filter file_original
This is a very niche application of git diff and usually, if you have already made the changes, then you might ask why you would need to apply that filter again. This command is useful if the changes need to be written as a filter and maybe shared among developers.
Applying git diff on branches
Nice thing about the git diff is that we are not limited to files. We can do the same with branches.
$ git diff branch_original branch_modified > filter
Make sure you are on your on your root directory level where your filter is located.
$ git apply filter branch_original
More info about git apply here
Unapplying git diff
Many git guides recommend to commit early and often, so if the filter doesn’t work, you can use git stash or git reset --hard to revert your changes.