Git's Wire Protocol

I use Git dozens of times a day and had never looked at what it sends over the network. git fetch prints a few lines about counting and resolving deltas, git push prints a tally, and the actual conversation between client and server stays hidden behind a progress bar. So I traced it: the bytes, in order, from pressing Return. This is what I found.

Short version: on a fetch, the client and server negotiate which commits each already has, then the server sends only the missing objects in one delta-compressed stream. The protocol carrying that conversation is older than most libraries used to talk to Git, and it was later moved onto HTTPS without changing its basic shape. It’s a framed, line-oriented protocol of the kind you could have written in 1995, and it has aged better than most of its contemporaries.

pkt-line: the framing everything sits in

Git’s wire protocol is made of pkt-lines. A pkt-line is a four-character hex length prefix followed by that many bytes, and the length includes the prefix itself. That’s the whole spec. Reading a real Git transcript just means parsing pkt-lines in your head.

For example:

001e# service=git-upload-pack\n

This pkt-line is 0x001e = 30 bytes long: the 4-byte prefix 001e plus 26 bytes of payload (# service=git-upload-pack and a \n).

Three special pkt-lines do a lot of the work:

  • 0000 is a flush packet: no payload, meaning “this section is done”.
  • 0001 is a delim packet (added in protocol v2): separates capability lines from arguments within one command.
  • 0002 is a response-end packet (also v2): marks the end of a response in a stateless connection where several command-responses share one transport.

Lengths from 0004 (empty payload, allowed but discouraged) to fff0 (65,520 bytes total, 65,516 of payload) carry data. Everything else is reserved.

A typical ref advertisement starts like this:

001e# service=git-upload-pack\n
0000
0148<sha1> HEAD\0multi_ack_detailed no-done side-band-64k thin-pack...\n
003d<sha1> refs/heads/main\n
0040<sha1> refs/heads/develop\n
0000

The first pkt-line announces the service and the first 0000 ends that section. Then the server sends one pkt-line per ref. The first ref is followed by a NUL byte and the server’s capability list. The second 0000 ends the advertisement.

Two things stand out. First, the capability list is squeezed into the first ref’s pkt-line instead of getting its own packet, a piece of 2005-era thrift that’s still there. Second, the framing is trivial to parse: read four bytes, parse the hex length, skip that many bytes, and you’re at the next pkt-line. No delimiter to search for and no escaping to undo. You don’t notice that property when a protocol is designed, but you appreciate it every time you write a packet sniffer.

upload-pack and receive-pack

Git’s network operations come in two kinds, and the names are backwards from the user’s point of view. git fetch and git clone run upload-pack on the server (the server uploads a pack to the client). git push runs receive-pack on the server (the server receives a pack). The verbs are from the server’s side.

I’ll mostly follow fetch, because that’s where the negotiation is, and the negotiation is the most interesting part.

A fetch has three phases:

  1. The server advertises its refs and capabilities.
  2. The client and server negotiate which commits they share, so the server doesn’t send history the client already has.
  3. The server builds a packfile with exactly the objects the client lacks and streams it back.

Phase 1 is simple and phase 3 is mostly file-format work. Phase 2 is where the engineering is.

The negotiation

The client wants some refs (say refs/heads/main) at a particular SHA-1. It has some history of its own that may or may not overlap with the server’s. Having the server send every commit reachable from the wanted ref would be wasteful. Having the client list every commit it has and ask the server to filter would also be wasteful.

Git’s negotiation sits between those. The client sends want lines (“I want commit X, and Y, and Z”) followed by a flush. Then it sends have lines (“I have commit A, B, C…”) in batches. For each batch, the server checks whether any of those commits are ancestors of objects reachable from the wants. If so, it sends an ACK for that commit and the negotiation has made progress.

The client doesn’t send every commit it has. It sends them in waves: branch tips first, then their parents, then grandparents, widening the search until the server has acknowledged enough to find a common ancestor, or until the client runs out of history and the server has to send everything from the roots.

A simple fetch looks like this:

0067want <sha1-of-wanted-commit> multi_ack_detailed side-band-64k thin-pack...\n
0032want <sha1-of-another-wanted-commit>\n
0000
0032have <sha1-of-local-commit-1>\n
0032have <sha1-of-local-commit-2>\n
...
0009done\n

The server replies ACK <sha1> for each have it recognizes, or NAK\n if nothing in the batch matched. When the client has nothing more useful to offer, or the server says it has found enough common history, the client sends done and the server commits to building a packfile.

There are three negotiation modes:

  • Base protocol (the oldest): the server sends a single ACK <sha1> for the first common commit it finds, and the client stops.
  • multi_ack: the server sends ACK <sha1> continue for each common commit, and the client decides when to stop.
  • multi_ack_detailed (what nearly every modern client uses): the server distinguishes common (we both have this commit) from ready (I have enough; send done when you like). The server can suggest when to stop but can’t require it.

The client’s choice of which haves to send is a bounded walk over its own commit graph. Git starts at each local ref tip and walks parents in roughly date order. It sends its first flush after 16 haves and widens the window each round after that. The standard client doesn’t wait between batches; it pipelines them, so the next batch is already on the wire when the server is ready to answer. Once the server has acknowledged enough (typically commits on every side of the merge frontier), the walk can stop. The optional skipping negotiator goes further and jumps over ever-larger stretches of history instead of offering every parent, and a set of common commits the server has acknowledged is used to prune the search. Users never see any of this, but it’s the difference between a fetch that sends fifty commits and one that sends fifty thousand.

I think this is the best design decision in the protocol. The negotiation is iterative, not enumerative. Neither side ever sends its whole graph. Each round trip narrows what the next one has to explore, and in the typical case the conversation converges in two or three round trips. The bad cases (branches with no common ancestor, fresh clones) fall back to sending everything reachable from the wants, which is the worst it can do anyway.

The packfile

Once the negotiation has settled which objects the client lacks, the server sends them as a packfile: one binary stream with every needed blob, tree, commit and tag, zlib-compressed and, where it helps, stored as a delta against another object.

The format is short:

  • a 12-byte header: PACK magic, a four-byte version and a four-byte object count;
  • the objects;
  • a 20-byte SHA-1 trailer checksumming the whole pack.

Each object starts with a variable-length header encoding its type (OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, OBJ_TAG, OBJ_OFS_DELTA or OBJ_REF_DELTA) and its uncompressed size. The body is zlib-compressed.

A delta object isn’t a complete object; it’s instructions for rebuilding one object from another. OBJ_OFS_DELTA says the base object is N bytes earlier in this pack; OBJ_REF_DELTA says the base is the object with SHA-1 X, which you should be able to find. The instructions are tiny: copy these bytes from the base, insert these literal bytes, copy these other bytes. For a file with one edited comment, the delta is a few dozen bytes plus a pointer to its base. That’s how a repository the size of the Linux kernel fits in gigabytes instead of terabytes.

A thin pack, the default for fetches, may use OBJ_REF_DELTA against objects the client already has but that aren’t in the pack. The client has to “fatten” the pack by resolving those external bases before it can index it. That looks like a compromise at first, but it’s the obvious choice: there’s no reason to ship a base object the client already has just to keep the pack self-contained.

After the pack arrives, the client indexes it by writing an .idx file: the SHA-1 of every object, its offset in the pack, and a fanout table keyed by the SHA-1’s first byte so lookups can binary-search within 1/256th of the index. Version 2 .idx files, the default since 2007, start with the magic \377tOc and a version word. On top of the fanout and SHA-1 table they add a CRC32 per object (to catch corruption when objects are copied between packs) and two offset tables, four bytes wide for the common case and eight bytes wide where needed, so packs over 2 GiB can be addressed. Until it’s indexed, a pack is just an opaque blob with a checksum at the end.

Smart HTTP

Git originally spoke git://, its own protocol on TCP port 9418, and SSH. Both worked fine on the open internet of 2005. Both got more awkward on corporate networks from about 2015 on, where outbound TCP to arbitrary ports is blocked and HTTPS on 443 is the only reliable path. Git’s answer was smart HTTP, which is how git clone https://github.com/... runs the whole pkt-line conversation over what looks to a firewall like ordinary web traffic.

Smart HTTP uses two endpoints:

  • GET /info/refs?service=git-upload-pack (or git-receive-pack): the server returns the same ref advertisement as the native protocol, prefixed with a # service=...\n pkt-line and ended with a flush. The Content-Type is application/x-git-upload-pack-advertisement, which tells caches in between not to touch it.
  • POST /git-upload-pack (or /git-receive-pack): the client sends its want/have conversation as the request body, as the same pkt-lines it would write to a socket, and gets the packfile back as the response body. The Content-Types are application/x-git-upload-pack-request and application/x-git-upload-pack-result.

The part that took me a moment: the protocol is stateless from HTTP’s point of view but stateful from Git’s. In v1, the whole want/have exchange for a round has to fit in one POST. The client can’t send have <sha1> in one request, get an ACK and decide what to send next in the same request. Instead it computes its opening offer locally and sends it in one body, and the server answers with what it can. If more rounds are needed, the client opens a new POST and sends its next offer, including everything from earlier rounds it now knows the server has. Each POST is a complete sub-conversation that starts with the client and ends with the server.

That’s not an efficient use of HTTP: every round trip is a new request, and while the TLS session may be reused, application state isn’t. It works anyway because the negotiation usually converges in one or two rounds, and an extra POST typically costs about a hundred milliseconds against seconds of pack streaming. Being tolerant of an inefficient transport turned out to be useful.

Protocol v2

Git introduced a second protocol version in 2018. It didn’t change the fundamentals, but it changed how the conversation starts.

The problem with v1 is the ref advertisement. Every time a client connects, the server sends every ref it has: every branch, every tag, every pull-request ref the hosting provider keeps in a hidden namespace. For a small repository that’s nothing. For a repository with three hundred thousand refs (they exist), it’s several megabytes of pkt-lines before the client has even said what it wants. git ls-remote suffers most, since it throws almost all of that away.

Protocol v2 reverses this. The client declares the version in whatever way the transport allows:

  • over ssh:// and file://, it sets GIT_PROTOCOL=version=2 in the remote process’s environment;
  • over native git://, it appends version=2 as an extra NUL-delimited parameter in the initial pkt-line request;
  • over HTTP, it sends a Git-Protocol: version=2 header.

The server, seeing v2, advertises its capabilities but not its refs. The client then issues an explicit command: ls-refs (optionally filtered by ref prefix), fetch or object-info. The server returns only what that command asked for.

A v2 conversation starts with a capability advertisement and no refs:

000eversion 2\n
0015agent=git/2.40.0\n
0013ls-refs=unborn\n
0025fetch=shallow filter ref-in-want\n
0012server-option\n
0017object-format=sha1\n
0000

The client sends a command pkt-line, then a delim packet (0001) before its arguments:

0014command=ls-refs\n
0015agent=git/2.40.0\n
0001
0009peel\n
000csymrefs\n
001bref-prefix refs/heads/\n
0000

ref-prefix limits the response to refs under refs/heads/, and the server returns only those. The advertisement goes from everything the server has to exactly what the client asked about, which on a large repository saves orders of magnitude of bandwidth.

The other notable v2 addition is ref-in-want. In v1, the client has to express wants as object IDs it learned from a ref advertisement. In v2 it can say want-ref refs/heads/main and the server resolves the ref itself. That removes a race v1 quietly tolerated (the ref could move between the client reading the advertisement and sending its wants), and it removes the need to advertise refs the client didn’t ask about.

v2 didn’t change the packfile format or the negotiation algorithm, only the opening. That was the right place to change things, because the opening was where the protocol’s age showed most: pkt-lines, packfiles and the iterative want/have exchange held up fine; the unconditional ref advertisement didn’t.

What I learned

  1. pkt-line framing holds the whole thing up. A four-byte hex length prefix is a modest choice, and that modesty is why everything else could change without touching the bytes underneath. New commands, new capabilities, new negotiation modes and a whole new protocol version were all added without changing the framing at all.
  2. The negotiation is the most interesting part, and the one you’ll never notice. git fetch sending fifty kilobytes after a routine pull, instead of the gigabytes in your repository, comes from a small, careful algorithm that walks the commit graph in batches and trades round trips for bandwidth.
  3. Git’s protocol avoids premature elegance. It isn’t RPC, gRPC or Protocol Buffers. It’s text-shaped binary with a hex length prefix and a flush packet, written by people more interested in being correct than modern. It’s been moved onto HTTPS, extended with capabilities and given a new version, and the original 2005 pkt-line is still the unit of every message. It’s hard to ask a protocol to age better.

To read further, the source is in Documentation/ in the Git repository, especially gitprotocol-common.adoc, gitprotocol-pack.adoc, gitprotocol-v2.adoc, gitprotocol-http.adoc and gitformat-pack.adoc. These lived at Documentation/technical/*.txt until Git 2.38 (2022) moved and renamed them, still as .txt; the switch to .adoc came later, around Git 2.49 (2025), so older posts cite the old paths. They aren’t light reading, but they’re the closest thing to a spec, and the bytes on the wire match them closely.