Skip to content

Instantly share code, notes, and snippets.

@pricees
Created September 25, 2018 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pricees/57fcbc2c258816f9cc1dbdf3f2627637 to your computer and use it in GitHub Desktop.
Save pricees/57fcbc2c258816f9cc1dbdf3f2627637 to your computer and use it in GitHub Desktop.
#Anatomy of a Git Diff
This is a **hunk**, not a *chunk*.
```
diff --git a/lorem b/lorem
index 5da0182..4fcdc38 100644
--- a/lorem
+++ b/lorem
@@ -16,4 +16,24 @@ Nullam congue nunc sollicitudin, fermentum urna vel, tempus lorem. Morbi molesti
```
The breakdown:
```
diff --git a/lorem b/lorem
```
This is a `diff` output...
using the `git` format (options: combined, raw, short, etc.)
between two files: `a/lorem` (source) and `b/lorem` (modified)
```
index 5da0182..4fcdc38 100644
```
These changes are being made to the git `index` file...
The shortened before and after hashes of the file states
and the permission level of the git `index` file
NOTE: This `index` keeps track of the file changes betwixt the working directory, staging area, and git repository.
```
--- a/lorem
+++ b/lorem
```
The source filename (`---`) and modified filename (`+++`). If adding a file, the source file will be `/dev/null`. If deleting a file, the modified file will be `/dev/null`
```
@@ -16,4 +16,24 @@ Nullam congue nunc sollicitudin, fermentum urna vel, tempus lorem. Morbi molesti
```
This is called the **unified diff hunk identifier**
This is a little confusing. Get ready:
`@@` Info delimiters
`-[source start line],[range of source lines in this hunk]` This is NOT the number of lines modified in the source file. It is the starting line and total number lines being displayed from the source file that appear in this hunk.
`+[modified start line],[range of modified lines in hunk]` This is NOT the number of lines modified/added in the modified file. It is the starting line and total number lines being displayed from the modified file that appear in this hunk.
`Nullam congue...` This is the header. The start line is the line after this.
Let's take a ganders at this I R L!!
# Git that Formatted Patch!
`git format-patch some_branch [-o directory]`
`git format-patch some_branch -1 [hash] -o patches`
Look at the format! Whaaa?!?!
We will use `git am` to apply multiple commits based on the patches. BTW: `am` means apply from **mailbox**.
# Gist apply the diff
Let's end with something easy:
`git apply` applies the `diff` without creating commits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment