codient/blog

How git push works

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To …/server.git
3d02722..fefe35c main -> main

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.

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.

The tiny repository
Click any object to see its real hash and what it points to.
Click c4 to start.

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:

$ git diff
def step_30(): return 30
-print('version 3')
+print('version 4')
$ git commit -am c5
[main fefe35c] c5
1 file changed, 1 insertion(+), 1 deletion(-)

c5 adds three new objects:

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.

Check yourself
c5 changed app.py. How many new objects does it add?

Five steps

Here are all the steps when I run git push:

  1. The server tells us what it has: its branches, and where each one points.
  2. The client sends the new commit: a pack with the new objects, and a request to move the branch.
  3. The server reads the pack: it rebuilds and hashes every object, and saves them in a temporary folder.
  4. The server checks nothing is missing: every object c5 needs is either in the pack or already on the server.
  5. The server updates the branch: if main is still where the client thought it was, it moves to c5.
Your laptopgit pushServerreceive-packServerunpack-objectsServerrev-list1my branches: main = 3d027222find what's newc5 → parent c4 (3d02722)server has c4: stopnew: c5, its tree, app.pymove main 3d02722 → fefe35cthen the pack: 3 objects, 281 bytes3here's the packread the packrebuild app.py from the old onehash each objecthave it already? skip itelse save it as a file…all in a temp folderdone4check fefe35cis anything missing?c5 → tree → app.pyeach one on the server?stop at what main hasnothing missing5move the branchtemp folder → objects folderlock mainis main still 3d02722?yes: main = fefe35cok refs/heads/mainIf step 3 or 4 fails, the temp folder is deleted,main doesn't move, and you get an error.

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:

receive-pack> 3d027221ab599b3a226b6c50d7dcb98831f34101 refs/heads/main

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:

push> 3d027221ab599b3a226b6c50d7dcb98831f34101 fefe35ca4132adb26831a4838342dab886a3a3ac refs/heads/main

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, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)

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:

fefe35c… commit 173 bytes 129 in the pack
6dc0d79… tree 71 bytes 79 in the pack
e540fe2… blob 751 bytes 41 in the pack (a delta)

Total 3 (delta 1). 3 objects sent, 1 of them as a delta.

Check yourself
Why doesn't the client send readme.md?

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:

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.

Check yourself
A buggy client sends c5 and its tree, but forgets the new app.py. What happens?

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.

receive-pack> unpack ok
receive-pack> ok refs/heads/main
To …/server.git
3d02722..fefe35c main -> main

main went from 3d02722 (c4) to fefe35c (c5).

If main had moved to a different commit, the push would be rejected.

Check yourself
What does the server check right before it moves main?

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:

$ find server.git/objects -type f
server.git/objects/6d/c0d799728f9db08b6ecd8510aa1087067ab17e tree
server.git/objects/e5/40fe29063964d5cb61eb3deec09b529b87b898 app.py
server.git/objects/fe/fe35ca4132adb26831a4838342dab886a3a3ac c5
server.git/objects/pack/pack-70cad741….pack c1 to c4
server.git/objects/pack/pack-70cad741….idx
server.git/objects/pack/pack-70cad741….bitmap
server.git/objects/pack/pack-70cad741….rev

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):

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.

Check yourself
A push has 150 objects. How does the server store it?

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 pushes at the same moment
Laptop A and laptop B both start at c4 and push at the same time. Step through what the server does.

Two clients, both at c4, push to main at the same moment. Only one push wins and moves main. The other gets an error:

remote: error: cannot lock ref 'refs/heads/main': is at a2e4887f… but expected 3d027221…
To …/server.git
! [remote rejected] main -> main (incorrect old value provided)
error: failed to push some refs to '…/server.git'

Laptop B has to fetch A's commit, put its change on top (for example with git pull --rebase), and push again.

Check yourself
Two clients at c4 push to main at the same moment. Which step decides who wins?

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.