logomichael sumner

How to Save Git Diff Into a File Using VS Code

Why would you want to do this?

Well, the main reason is that within VS code there currently is no way to effectively save your Git Diff into a file, that I know of. If you do, contact me!

Another reason is that you want to display the differences between files without needing to create a repository of your own. If you just have small changes to make, then a diff or patch file will do.

There are VS code extensions that have worked in the past. But for those of you who are encountering issues with those extensions, then this article may be suited for you.

So here is how to save a git diff, as a file.

Let’s dive in.

  1. Open VS Code.
  2. Make sure that you are in your working repository, or that you have already initialised your repository.
  3. Make changes to your files.
  4. Stage your files. A nice way of doing so is by installing the VS Code extension called Git Lens.
  5. Open the VS Code terminal.
  6. And type git diff --staged > mydiff.diff to create your diff file.
  7. If you want to make a patch file, type git diff --patch --staged > mypatch.patch
  8. You should now have a specified file saved within your current working repository.

Contributing to WordPress Core

One of the reasons that you might want to use a diff or a patch file (they’re basically the same and doesn’t matter what you call it — a patch is nice) if you want to contribute to the WordPress core.

You might have seen a few videos of how to contribute to the WordPress core — ultimately improving the lives of many developers and businesses — but you might not have found that VS Code doesn’t have the diff or patch utility to save it as a file.

VS Code can certainly do so when it comes to managing a repository in the most developer-friendly way, but simple things like saving a diff or patch file can be left to the unknown.

Hopefully this guide has helped you learn something new.

Here is an example of a git diff or patch file:

I am using an example contribution to the WordPress Core. You will notice that the plus and minus symbols +++ and — indicate what has been added or removed:

diff --git a/src/wp-includes/class-wp-hook.php b/src/wp-includes/class-wp-hook.php
index 432a01600f..03119724f1 100644
--- a/src/wp-includes/class-wp-hook.php
+++ b/src/wp-includes/class-wp-hook.php
@@ -71,6 +71,9 @@ final class WP_Hook implements Iterator, ArrayAccess {
 	 * @param int      $accepted_args The number of arguments the function accepts.
 	public function add_filter( $hook_name, $callback, $priority, $accepted_args ) {
+		$hook_name = trim( $hook_name );
 		$idx = _wp_filter_build_unique_id( $hook_name, $callback, $priority );
 		$priority_existed = isset( $this->callbacks[ $priority ] );
diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php
index 5f4fa15986..4f6988be74 100644
--- a/src/wp-includes/plugin.php
+++ b/src/wp-includes/plugin.php
@@ -114,6 +114,8 @@ if ( ! isset( $wp_current_filter ) ) {
 function add_filter( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
 	global $wp_filter;
+	$hook_name = trim( $hook_name );
 	if ( ! isset( $wp_filter[ $hook_name ] ) ) {
 		$wp_filter[ $hook_name ] = new WP_Hook();