Builds take very long in "Preparing Git Reference refs/heads/main"

Sometimes builds take very long in “Preparing Git Reference refs/heads/main” (about 11 minutes).

Everything else in the build is quick, this particular build took 13m 26s so those 11 minutes take up a huge overhead. Any idea how to prevent this and what could be causing it?

10:22:39 AM: Build ready to start
10:22:42 AM: build-image version: d2c6dbeac570350a387d832f64bc980dc964ad65 (focal)
10:22:42 AM: build-image tag: v4.8.0
10:22:42 AM: buildbot version: 29e546ddee264a16af472b38ccba15b350150213
10:22:43 AM: Fetching cached dependencies
10:22:43 AM: Starting to download cache of 3.3GB
10:24:04 AM: Finished downloading cache in 1m21.876551694s
10:24:04 AM: Starting to extract cache
10:24:51 AM: Finished extracting cache in 46.471719878s
10:24:51 AM: Finished fetching cache in 2m8.509831336s
10:24:51 AM: Starting to prepare the repo for build
10:24:51 AM: Preparing Git Reference refs/heads/main
10:35:06 AM: Parsing package.json dependencies
10:35:07 AM: Starting build script
10:35:07 AM: Installing dependencies
10:35:07 AM: Python version set to 2.7
10:35:08 AM: Started restoring cached node version
10:35:18 AM: Finished restoring cached node version
10:35:18 AM: v16.15.0 is already installed.
10:35:19 AM: Now using node v16.15.0 (npm v8.5.5)
10:35:19 AM: Started restoring cached build plugins
10:35:19 AM: Finished restoring cached build plugins
10:35:19 AM: Attempting ruby version 2.7.2, read from environment
10:35:20 AM: Using ruby version 2.7.2
10:35:20 AM: Using PHP version 8.0
10:35:21 AM: No npm workspaces detected
10:35:21 AM: Started restoring cached node modules
10:35:21 AM: Finished restoring cached node modules
10:35:21 AM: Started restoring cached go cache
10:35:21 AM: Finished restoring cached go cache
10:35:21 AM: go version go1.16.5 linux/amd64
10:35:21 AM: go version go1.16.5 linux/amd64
10:35:21 AM: Installing missing commands
10:35:21 AM: Verify run directory
10:35:22 AM: ​
10:35:22 AM: ────────────────────────────────────────────────────────────────
10:35:22 AM:   Netlify Build                                                 
10:35:22 AM: ────────────────────────────────────────────────────────────────
10:35:22 AM: ​
10:35:22 AM: ❯ Version
10:35:22 AM:   @netlify/build 27.1.1
10:35:22 AM: ​
10:35:22 AM: ❯ Flags
10:35:23 AM:   baseRelDir: true
10:35:23 AM:   buildId: 627e154f99449c000995099c
10:35:23 AM:   deployId: 627e154f99449c000995099e
10:35:23 AM: ​
10:35:23 AM: ❯ Current directory
10:35:23 AM:   /opt/build/repo
10:35:23 AM: ​
10:35:23 AM: ❯ Config file
10:35:23 AM:   /opt/build/repo/netlify.toml
10:35:23 AM: ​
10:35:23 AM: ❯ Context
10:35:23 AM:   production
10:35:23 AM: ​
10:35:23 AM: ❯ Loading plugins
10:35:23 AM:    - netlify-plugin-cache@1.0.3 from netlify.toml and package.json
10:35:24 AM: ​
10:35:24 AM: ────────────────────────────────────────────────────────────────
10:35:24 AM:   1. netlify-plugin-cache (onPreBuild event)                    
10:35:24 AM: ────────────────────────────────────────────────────────────────
10:35:24 AM: ​
10:35:26 AM: Successfully restored: .cache, _site ... 152 files in total.
10:35:26 AM: ​
10:35:26 AM: (netlify-plugin-cache onPreBuild completed in 1.6s)
10:35:26 AM: ​
10:35:26 AM: ────────────────────────────────────────────────────────────────
10:35:26 AM:   2. Build command from Netlify app                             
10:35:26 AM: ────────────────────────────────────────────────────────────────
10:35:26 AM: ​
10:35:26 AM: $ eleventy
10:35:59 AM: Copied 751 files / Wrote 218 files in 30.46 seconds (139.7ms each, v0.12.1)
10:35:59 AM: ​
10:35:59 AM: (build.command completed in 33.3s)
10:35:59 AM: ​
10:35:59 AM: ────────────────────────────────────────────────────────────────
10:35:59 AM:   3. Deploy site                                                
10:35:59 AM: ────────────────────────────────────────────────────────────────
10:35:59 AM: ​
10:35:59 AM: Starting to deploy site from '_site'
10:36:00 AM: Creating deploy tree asynchronously
10:36:02 AM: 159 new files to upload
10:36:02 AM: 0 new functions to upload
10:36:42 AM: Post processing - header rules
10:36:42 AM: Post processing - redirect rules
10:36:42 AM: Post processing done
10:36:45 AM: Site is live ✨
10:36:47 AM: Finished waiting for live deploy in 40.062106676s
10:36:47 AM: Site deploy was successfully initiated
10:36:47 AM: ​
10:36:47 AM: (Deploy site completed in 47.8s)
10:36:47 AM: ​
10:36:47 AM: ────────────────────────────────────────────────────────────────
10:36:47 AM:   4. netlify-plugin-cache (onSuccess event)                     
10:36:47 AM: ────────────────────────────────────────────────────────────────
10:36:47 AM: ​
10:36:49 AM: Successfully cached: .cache, _site ... 152 files in total.
10:36:49 AM: ​
10:36:49 AM: (netlify-plugin-cache onSuccess completed in 2.5s)
10:36:49 AM: ​
10:36:49 AM: ────────────────────────────────────────────────────────────────
10:36:49 AM:   Netlify Build Complete                                        
10:36:49 AM: ────────────────────────────────────────────────────────────────
10:36:49 AM: ​
10:36:49 AM: (Netlify Build completed in 1m 26.9s)
10:36:50 AM: Caching artifacts
10:36:50 AM: Started saving node modules
10:36:50 AM: Finished saving node modules
10:36:50 AM: Started saving build plugins
10:36:50 AM: Finished saving build plugins
10:36:50 AM: Started saving pip cache
10:36:50 AM: Finished saving pip cache
10:36:50 AM: Started saving emacs cask dependencies
10:36:50 AM: Finished saving emacs cask dependencies
10:36:50 AM: Started saving maven dependencies
10:36:50 AM: Finished saving maven dependencies
10:36:50 AM: Started saving boot dependencies
10:36:50 AM: Finished saving boot dependencies
10:36:50 AM: Started saving rust rustup cache
10:36:50 AM: Finished saving rust rustup cache
10:36:50 AM: Started saving go dependencies
10:36:50 AM: Finished saving go dependencies
10:36:50 AM: Build script success
10:59:17 AM: Failed to save cache, continuing with build
10:59:18 AM: Finished processing build request in 36m35.538566343s

If I clear cache and rebuild the whole process takes about 3mins, and then the next time it builds and loads from cache the same issue occurs with 13–15mins of buildtime.

Hi reedyn,

give these guides a read through, they contain a bunch of information on how to optimize your site’s builds!