How git push works
What happens when you run git push? What does the client (your machine) do, and what does the server do? What happens when lots of pushes arrive at the same time?
In earlier posts (part 1, part 2) we looked at how git clone works. git push is kinda the same but in reverse.
- The client (git running on your machine) builds the pack file and sends it to the server
- The server has to check and verify the pack before it moves the branch.
How git push works
Let's look at a small repository. It has four commits, c1 to c4, and two files, readme.md and app.py. The main branch points to c4.
A quick refresher. Git stores objects: commits, trees (folder listings) and blobs (file contents). Each object is named by a hash of its contents (plus its type and size). A pack is one file holding many objects.
Let's make one change to app.py and commit it as c5 locally:
c5 adds three new objects:
- the commit c5 (
fefe35c) - its tree (
6dc0d79) - the new blob for
app.py(e540fe2)
c5's tree also points at readme.md, but that blob didn't change. It's the same one c4 has, so it isn't new.
Five steps
Here are all the steps when I run git push:
- The server tells us what it has: its branches, and where each one points.
- The client sends the new commit: a pack with the new objects, and a request to move the branch.
- The server reads the pack: it rebuilds and hashes every object, and saves them in a temporary folder.
- The server checks nothing is missing: every object c5 needs is either in the pack or already on the server.
- The server updates the branch: if main is still where the client thought it was, it moves to c5.
I ran GIT_TRACE_PACKET=1 git push to see the trace for each step. Let's go through it step by step.
Step 1: the server tells us what it has
The git server runs git receive-pack, the command that handles pushes. It lists its branches and where each one points:
So the server's main is at 3d02722. That's c4.
Step 2: the client sends the new commit
Git on my laptop (the client) starts at c5 and walks back the graph. c5's parent is c4, which the server already has (step 1 told us). So it stops there.
First, the client sends one line:
That line means: "move main from 3d02722 to fefe35c."
Then it builds a pack and sends it. This step corresponds to the first lines of the output we saw in the terminal:
Enumerating objects, 5. Start at c5 and walk the git object graph back to c4 (latest commit on main). The git client builds a list as it is walking the graph: the list has the 3 new objects, plus 2 the server already has, c4's tree and the old app.py. The two existing objects are added so they can be used to create the delta object.
Counting objects, 5. The git client looks up each object on the list: what kind it is, how big it is, and where it's stored.
Compressing objects, 3. The delta search. The client compares each of the 3 objects it will send with similar objects on the list. The server already has the old app.py (02d845b, from c4). So instead of sending the whole file, the client sends a delta: which parts of the old app.py to copy, plus the few bytes that changed. Part 2 covers deltas.
Writing objects, 3, 281 bytes. The client creates the pack. Each object is compressed and written into the pack. app.py is written as a delta:
Total 3 (delta 1). 3 objects sent, 1 of them as a delta.
Step 3: the server reads the pack
receive-pack hands the pack to another git command, git unpack-objects. (That's for a push with fewer than 100 objects. Bigger pushes go to index-pack)
unpack-objects does the following:
- Read each object out of the pack.
app.pycame as a delta, so it rebuilds the full file from the oldapp.pythe server already has. - Calculate the hash of each object.
- Check: do I already have an object with this hash? If yes, skip it.
- Otherwise, save it as its own file, in a temporary folder.
The rest of the server can't see the objects in the temporary folder yet. If step 3 or 4 fails, the server deletes the folder and nothing has changed.
Step 4: the server checks nothing is missing
Another git command, git rev-list, walks the graph starting at c5. It checks to make sure each object exists, either in the temporary folder or on the server.
It stops at c4 since that main already points to it.
The server fails a push if an object is missing from the pack.
Step 5: the server updates the branch
Once steps 3 and 4 pass, the objects move from the temporary folder into the server's objects folder.
Then the server locks main. It creates a file called main.lock, so no other push can change main in the meantime. It checks whether main still points to 3d02722. If yes, main is updated to point to fefe35c. The lock is released after the update.
main went from 3d02722 (c4) to fefe35c (c5).
If main had moved to a different commit, the push would be rejected.
Where c5 ends up
A push can be stored on the server as loose objects or as a pack. Git decides based on a setting called receive.unpackLimit. Its default is 100.
A push with fewer than 100 objects gets unpacked into loose objects: one small file per object. For c5, that's 3 files. Each one lives in a folder named after the first two characters of its hash:
A push with 100 or more objects stays as a pack. index-pack handles it instead of unpack-objects, and it creates three new files in the server's pack folder (objects/pack):
- A pack file (
.pack) with the compressed objects from that push. If the push sent a delta against an object the server already has, git adds a copy of that object to the pack, so the pack can be read on its own. - An index file (
.idx): a sorted list of the hashes in the pack, and where each object sits in it. That's how the server finds one object without reading the whole pack. - A reverse index (
.rev): for each object, in the order they sit in the pack, where to find it in the.idx. Git uses it to work out each object's size on disk, and when it reuses objects from the pack.
Loose files add up with every push. After each push, receive-pack starts git maintenance run --auto. Once there are roughly 6,700 loose objects, that packs them up.
Many pushes at once
Every push gets its own receive-pack and its own temporary folder. So two pushes can read and check their packs side by side, without waiting for each other. (They still share the server's CPU and disk.)
Here's what happens when two git clients push at the same moment:
Two clients, both at c4, push to main at the same moment. Only one push wins and moves main. The other gets an error:
Laptop B has to fetch A's commit, put its change on top (for example with git pull --rebase), and push again.
Correctness is the easy part: git never lets two pushes both move main. What all those refused pushes cost the server is for the next post.