# How to Cache the Source Code directory

**URL:** <https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533>\
**Category:** Caching\
**Tags:** build-cache\
**Created:** [April 22, 2017, 7:38am UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533 "2017-04-22T07:38:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [April 22, 2017, 7:38am UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/1 "2017-04-22T07:38:47Z")

</div>

Bitrise does not do automatic git/source code caching, unless you configure it for yourself. This is part of our Security policy too (that we don’t store your code and we never access it outside of the ephemeral build VMs): [Redirecting…](http://devcenter.bitrise.io/getting-started/code-security/#source-code)

You can of course configure the source to be cached, a very simple / minimal example:

```auto
workflows:
  primary:
    steps:
    - activate-ssh-key@3.1.1:
    - cache-pull@0.9.2: {}
    - git-clone@3.4.3:
        inputs:
        - reset_repository: 'Yes'
    - cache-push@0.9.4:
        inputs:
        - cache_paths: "$BITRISE_SOURCE_DIR"

```

​Basically you should add the **Cache:Pull** step before the **Git Clone** step, and a **Cache:Push** step after the Git Clone step.  
If all you want to cache is the src dir then **Cache:Push** can be right after **Git Clone** , if you’ll want to cache other things then move it to be the last step in the workflow (or when all the things you want to cache are available).

So, once you added the **Cache:Push** and **Cache:Pull** steps:

1. Select the **Cache:Push** step, and specify the **Cache paths** input of the step: `$BITRISE_SOURCE_DIR`
2. That’s pretty much all what’s required, but it’s also a good idea to enable the **Git Clone** step’s “clear before checkout” option
  1. select the **Git Clone** step
  2. and search for the **Reset repository** option
  3. then set it to **Yes**.
  4. _This will ensure that the step will clear out any previously checked out files before the new checkout (e.g. to remove files which got `.gitignore`d, but were not in the previously cached state, and other similar edge cases)._

If you have any questions just let us know! 😉

---

<div class="post-metadata">

**Author:** ![rrmori](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@rrmori](https://discuss.bitrise.io/u/rrmori)\
**Post date:** [May 14, 2019, 4:38pm UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/2 "2019-05-14T16:38:42Z")

</div>

Hi @viktorbenei,

Thanks for your post and sorry if my one is a silly questions but I’m newbie in Bitrise and for me it’s not clear how the Bitrise cache works. My questions are:

1. In the example scripts reported previously in this page and in all the example I found on internet, when we call the cache-pull we don’t report any source/cache folder as input, but in cache push is required the cache\_path, is the pull script pulling everything is cached?
2. Is it possible to create several cache in the same script and in the same branch? For example one cache for node\_modules, one for ruby gems, one for the cordova plugins… if it’s possible, is there an example how to do it? Can we keep it independents each other?
3. Is it possible to define which folder we want to push/pull and where we store the cache?

Thanks in advance for your answers.

---

<div class="post-metadata">

**Author:** ![fehersanyi-bitrise](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fehersanyi-bitrise/32/1912_2.png) [@fehersanyi-bitrise](https://discuss.bitrise.io/u/fehersanyi-bitrise)\
**Post date:** [May 17, 2019, 10:52am UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/3 "2019-05-17T10:52:21Z")

</div>

Hy there, sorry for the late reply

1, the cache pull will pull the cached things into the folder the previous step got them from (i don’t really understand how, it’s magic)

2, gems and cordova plugins are cached by default if you have the steps in, node\_modules is not advised to be cached because it in most cases is filled with garbage dependencies and can cause errors.

3, you can define what to cache in the cache push step, we store it on our own servers

---

<div class="post-metadata">

**Author:** ![mihaela.lorincz](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/mihaela.lorincz/32/3037_2.png) [@mihaela.lorincz](https://discuss.bitrise.io/u/mihaela.lorincz)\
**Post date:** [June 24, 2020, 3:21pm UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/4 "2020-06-24T15:21:29Z")

</div>

Hi, couple of questions here:

1. You state that cache pull is before git clone step but [documentation](https://devcenter.bitrise.io/builds/caching/about-caching-index/) states something else: " You should not put the **[Bitrise.io](http://Bitrise.io) Cache:Pull** Step BEFORE the **Git Clone Repository** Step". A little bit confused here, can you explain which one is correct, or when to use one approach or the other?

2. I tried saving the last commit sha in $BITRISE\_CACHE\_DIR and take it on the next build to use it for [fastlane change log](https://docs.fastlane.tools/actions/changelog_from_git_commits/) (release notes) like [here](http://discuss.bitrise.io/t/global-variable-incremented-in-every-build/1562/4). But $BITRISE\_CACHE\_DIR changes at each build so the file path is not found. Then tried the approach with $BITRISE\_SOURCE\_DIR like [here](http://discuss.bitrise.io/t/bitrise-cache-dir-seems-to-change-between-builds/8604/2) but still the file is not found between builds even if Reset Repository is set to No. This are the logs:

Not sure what i am missing here. Can you please help?

---

<div class="post-metadata">

**Author:** ![Roland-Bak](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/roland-bak/32/3480_2.png) [@Roland-Bak](https://discuss.bitrise.io/u/Roland-Bak)\
**Post date:** [June 30, 2020, 10:05am UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/5 "2020-06-30T10:05:13Z")

</div>

Hey there @mihaela.lorincz 👋

Could you please send us the URL of the build and also enable Support Access on the Settings tab of the app ([Enabling the Bitrise Support user for your app | Bitrise DevCenter](https://devcenter.bitrise.io/troubleshooting/enabling-bitrise-support-user/)), so that we may take a closer look? 🙂

---

<div class="post-metadata">

**Author:** ![mihaela.lorincz](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/mihaela.lorincz/32/3037_2.png) [@mihaela.lorincz](https://discuss.bitrise.io/u/mihaela.lorincz)\
**Post date:** [June 30, 2020, 1:09pm UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/6 "2020-06-30T13:09:22Z")

</div>

Hi @Roland-Bak . The support access is enabled and build link is: [https://app.bitrise.io/build/470bbd8b38bdf6e3](https://app.bitrise.io/build/470bbd8b38bdf6e3). Thanks for help

---

<div class="post-metadata">

**Author:** ![GMosdall](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/gmosdall/32/3321_2.png) [@GMosdall](https://discuss.bitrise.io/u/GMosdall)\
**Post date:** [January 8, 2021, 2:34pm UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/7 "2021-01-08T14:34:26Z")

</div>

I’m still confused, should you put **Cache:Pull** before **Git Clone** or not?  
These instructions contradict this [documentation](https://devcenter.bitrise.io/builds/caching/about-caching-index/) about caching.

---

<div class="post-metadata">

**Author:** ![chaitanya.tanna](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@chaitanya.tanna](https://discuss.bitrise.io/u/chaitanya.tanna)\
**Post date:** [January 19, 2021, 9:44pm UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/8 "2021-01-19T21:44:15Z")

</div>

Hi @GMosdall  
Cache:pull step should after the Git clone step.  
The cache is stored for the 7 days.

Thanks  
Chaitanya

---

<div class="post-metadata">

**Author:** ![adigladi](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/adigladi/32/3591_2.png) [@adigladi](https://discuss.bitrise.io/u/adigladi)\
**Post date:** [October 14, 2021, 11:59am UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/9 "2021-10-14T11:59:16Z")

</div>

Can we get some clarification here, can Git LFS be cached using the Cache:pull step after the Git clone step? Does anyone have an example on how to cache Git LFS?

---

<div class="post-metadata">

**Author:** ![adigladi](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/adigladi/32/3591_2.png) [@adigladi](https://discuss.bitrise.io/u/adigladi)\
**Post date:** [October 18, 2021, 3:19pm UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/10 "2021-10-18T15:19:32Z")

</div>

If anyone wonders in the future, adding `.git/lfs` to paths in the cache step worked for me.

---

<div class="post-metadata">

**Author:** ![chrishs](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@chrishs](https://discuss.bitrise.io/u/chrishs)\
**Post date:** [June 3, 2022, 11:52am UTC](https://discuss.bitrise.io/t/how-to-cache-the-source-code-directory/1533/11 "2022-06-03T11:52:06Z")

</div>

Would you be so kind and share the steps of your bitrise.yml? It’s in Workflow editor, last tab, or committed to your repository. Hope I don’t need to reinvent the wheel.

I assume you configured it so it skips downloading LFS files if they are cached already, but if they are not cached or different, save it to cache, so next time they don’t need to be redownloaded.

Really, really Appreciate!
