| 1 | # Git LFS Specification |
| 2 | |
| 3 | This is a general guide for Git LFS clients. Typically it should be |
| 4 | implemented by a command line `git-lfs` tool, but the details may be useful |
| 5 | for other tools. |
| 6 | |
| 7 | ## The Pointer |
| 8 | |
| 9 | The core Git LFS idea is that instead of writing large blobs to a Git repository, |
| 10 | only a pointer file is written. |
| 11 | |
| 12 | * Pointer files are text files which MUST contain only UTF-8 characters. |
| 13 | * Each line MUST be of the format `{key} {value}\n` (trailing unix newline). |
| 14 | * Only a single space character between `{key}` and `{value}`. |
| 15 | * Keys MUST only use the characters `[a-z] [0-9] . -`. |
| 16 | * The first key is _always_ `version`. |
| 17 | * Lines of key/value pairs MUST be sorted alphabetically in ascending order |
| 18 | (with the exception of `version`, which is always first). |
| 19 | * Values MUST NOT contain return or newline characters. |
| 20 | * Pointer files MUST be stored in Git with their executable bit matching that |
| 21 | of the replaced file. |
| 22 | * Pointer files must be less than 1024 bytes in size, including any pointer |
| 23 | extension lines. |
| 24 | * Pointer files are unique: that is, there is exactly one valid encoding for a |
| 25 | pointer file. |
| 26 | |
| 27 | An empty file is the pointer for an empty file. That is, empty files are |
| 28 | passed through LFS without any change. |
| 29 | |
| 30 | The required keys are: |
| 31 | |
| 32 | * `version` is a URL that identifies the pointer file spec. Parsers MUST use |
| 33 | simple string comparison on the version, without any URL parsing or |
| 34 | normalization. It is case sensitive, and %-encoding is discouraged. |
| 35 | * `oid` tracks the unique object id for the file, prefixed by its hashing |
| 36 | method: `{hash-method}:{hash}`. Currently, only `sha256` is supported. The |
| 37 | hash is lower case hexadecimal. |
| 38 | * `size` is in bytes. |
| 39 | |
| 40 | Example of a v1 text pointer: |
| 41 | |
| 42 | ``` |
| 43 | version https://git-lfs.github.com/spec/v1 |
| 44 | oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 45 | size 12345 |
| 46 | (ending \n) |
| 47 | ``` |
| 48 | |
| 49 | Blobs created with the pre-release version of the tool generated files with |
| 50 | a different version URL. Git LFS can read these files, but writes them using |
| 51 | the version URL above. |
| 52 | |
| 53 | ``` |
| 54 | version https://hawser.github.com/spec/v1 |
| 55 | oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 56 | size 12345 |
| 57 | (ending \n) |
| 58 | ``` |
| 59 | |
| 60 | For testing compliance of any tool generating its own pointer files, the |
| 61 | reference is this official Git LFS tool: |
| 62 | |
| 63 | **NOTE:** exact pointer command behavior TBD! |
| 64 | |
| 65 | * Tools that parse and regenerate pointer files MUST preserve keys that they |
| 66 | don't know or care about. |
| 67 | * Run the `pointer` command to generate a pointer file for the given local |
| 68 | file: |
| 69 | |
| 70 | ``` |
| 71 | $ git lfs pointer --file=path/to/file |
| 72 | Git LFS pointer for path/to/file: |
| 73 | |
| 74 | version https://git-lfs.github.com/spec/v1 |
| 75 | oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 76 | size 12345 |
| 77 | ``` |
| 78 | |
| 79 | * Run `pointer` to compare the blob OID of a pointer file built by Git LFS with |
| 80 | a pointer built by another tool. |
| 81 | |
| 82 | * Write the other implementation's pointer to "other/pointer/file": |
| 83 | |
| 84 | ``` |
| 85 | $ git lfs pointer --file=path/to/file --pointer=other/pointer/file |
| 86 | Git LFS pointer for path/to/file: |
| 87 | |
| 88 | version https://git-lfs.github.com/spec/v1 |
| 89 | oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 90 | size 12345 |
| 91 | |
| 92 | Blob OID: 60c8d8ab2adcf57a391163a7eeb0cdb8bf348e44 |
| 93 | |
| 94 | Pointer from other/pointer/file |
| 95 | version https://git-lfs.github.com/spec/v1 |
| 96 | oid sha256 4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 97 | size 12345 |
| 98 | |
| 99 | Blob OID: 08e593eeaa1b6032e971684825b4b60517e0638d |
| 100 | |
| 101 | Pointers do not match |
| 102 | ``` |
| 103 | |
| 104 | * It can also read STDIN to get the other implementation's pointer: |
| 105 | |
| 106 | ``` |
| 107 | $ cat other/pointer/file | git lfs pointer --file=path/to/file --stdin |
| 108 | Git LFS pointer for path/to/file: |
| 109 | |
| 110 | version https://git-lfs.github.com/spec/v1 |
| 111 | oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 112 | size 12345 |
| 113 | |
| 114 | Blob OID: 60c8d8ab2adcf57a391163a7eeb0cdb8bf348e44 |
| 115 | |
| 116 | Pointer from STDIN |
| 117 | version https://git-lfs.github.com/spec/v1 |
| 118 | oid sha256 4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 119 | size 12345 |
| 120 | |
| 121 | Blob OID: 08e593eeaa1b6032e971684825b4b60517e0638d |
| 122 | |
| 123 | Pointers do not match |
| 124 | ``` |
| 125 | |
| 126 | ## Intercepting Git |
| 127 | |
| 128 | Git LFS uses the `clean` and `smudge` filters to decide which files use it. The |
| 129 | global filters can be set up with `git lfs install`: |
| 130 | |
| 131 | ``` |
| 132 | $ git lfs install |
| 133 | ``` |
| 134 | |
| 135 | These filters ensure that large files aren't written into the repository proper, |
| 136 | instead being stored locally at `.git/lfs/objects/{OID-PATH}` (where `{OID-PATH}` |
| 137 | is a sharded filepath of the form `OID[0:2]/OID[2:4]/OID`), synchronized with |
| 138 | the Git LFS server as necessary. Here is a sample path to a file: |
| 139 | |
| 140 | .git/lfs/objects/4d/7a/4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393 |
| 141 | |
| 142 | The `clean` filter runs as files are added to repositories. Git sends the |
| 143 | content of the file being added as STDIN, and expects the content to write |
| 144 | to Git as STDOUT. |
| 145 | |
| 146 | * Stream binary content from STDIN to a temp file, while calculating its SHA-256 |
| 147 | signature. |
| 148 | * Atomically move the temp file to `.git/lfs/objects/{OID-PATH}` if it does not |
| 149 | exist, and the sha-256 signature of the contents matches the given OID. |
| 150 | * Delete the temp file. |
| 151 | * Write the pointer file to STDOUT. |
| 152 | |
| 153 | Note that the `clean` filter does not push the file to the server. Use the |
| 154 | `git push` command to do that (lfs files are pushed before commits in a pre-push hook). |
| 155 | |
| 156 | The `smudge` filter runs as files are being checked out from the Git repository |
| 157 | to the working directory. Git sends the content of the Git blob as STDIN, and |
| 158 | expects the content to write to the working directory as STDOUT. |
| 159 | |
| 160 | * Read 100 bytes. |
| 161 | * If the content is ASCII and matches the pointer file format: |
| 162 | * Look for the file in `.git/lfs/objects/{OID-PATH}`. |
| 163 | * If it's not there, download it from the server. |
| 164 | * Write its contents to STDOUT |
| 165 | * Otherwise, simply pass the STDIN out through STDOUT. |
| 166 | |
| 167 | The `.gitattributes` file controls when the filters run. Here's a sample file that |
| 168 | runs all mp3 and zip files through Git LFS: |
| 169 | |
| 170 | ``` |
| 171 | $ cat .gitattributes |
| 172 | *.mp3 filter=lfs -text |
| 173 | *.zip filter=lfs -text |
| 174 | ``` |
| 175 | |
| 176 | Use the `git lfs track` command to view and add to `.gitattributes`. |