Skip to content

git diff {filename} local.patch

Robert N edited this page Oct 25, 2015 · 1 revision

Get a patch file for a single file changed in the working directory (i.e. not committed):

Git

$ git diff myChangedFile.as > myChangedFile.patch

LibGit2Sharp

var patch = repo.Diff.Compare<Patch> (new List<string>() { "myChangedFile.as" });

Complete cut/paste example:

using System;
using System.Collections.Generic;
using LibGit2Sharp;

namespace libgitdiff
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			var repo = new Repository ("/your/repo/path");
			foreach (var item in repo.RetrieveStatus()) {
				if (item.State == FileStatus.Modified) {
					var patch = repo.Diff.Compare<Patch> (new List<string>() { item.FilePath });
					Console.WriteLine ("~~~~ Patch file ~~~~");
					Console.WriteLine (patch.Content);
				}
			}
		}
	}
}
Clone this wiki locally