How to export Overleaf v2 project as a git repository

1.1k views Asked by At

I want to see the history of my Overleaf v2 document in my favourite diff viewer, so I want to export it as a git repository. Unfortunately, Overleaf v2 has no git support yet (at least not for the general public).

How do I export my document's history as a git repository?

1

There are 1 answers

1
Christallkeks On

You'll need curl, unzip and a bash shell.

  1. Login to Overleaf in your browser and open your document: https://www.overleaf.com/project/YOUR-PROJECT-ID

  2. Click "History" in the top right corner and select the most recent change.

  3. Open the dev tools of your browser (Ctrl+Shift+K in Firefox, F12 in Chrome)

  4. Click on the "Download project at this version" link. Your browser opens https://www.overleaf.com/project/YOUR-PROJECT-ID/version/MAX/zip, where MAX is the incremental ID of your most recent revision (probably a fairly high integer, it was 413 for me, and that was a project I just started working on). You can cancel the download, we don't actually care about it.

  5. Using the dev tools, look at the cookies your browser sent. There should be five of them: _ga, _gid, overleaf_session, SERVERID, and sixpack-clientId.

  6. Open a shell and do mkdir overleaf-git-export and cd overleaf-git-export

  7. Bulk download all ZIP snapshots of your project with CURL. You have to set the cookies you just inspected in the browser (obviously I shortened mine for this example) and set the MAX variable you found in the URL: curl --cookie "_ga=GA1...;_gid=GA1...;overleaf_session=meRG...;SERVERID=sl-l...;sixpack-clientId=5c17..." https://www.overleaf.com/project/YOUR-PROJECT-ID/version/[1-MAX]/zip -o zip#1.zip This can take a while (my 413 revisions needed 20min).

  8. Initialise our working folder for the script we'll be using soon: mkdir gitsave, mkdir repo, cd repo, git init, cd ..

  9. Then use this script (note that you'll have to adjust the MAX variable and the paths to your overleaf-git-export folder). It incrementally replaces the content of the repo with the content of the revision and commits it, fully wiping the repo in between but of course preserving the .git folder which contains the git history we want. This was surprisingly quick for me (a few minutes for all 413).

#!/bin/bash
for i in `seq 1 MAX`;
do
    cd ~/Desktop/overleaf-git-export
    rm -r -f gitsave
    cp -r repo/.git gitsave
    rm -r -f repo
    unzip zip$i.zip -d zip$i
    cp -r zip$i repo
    cp -r gitsave repo/.git
    cd ~/Desktop/overleaf-git-export/repo
    git add *
    git commit -m "v$i"
    cd ~/Desktop/overleaf-git-export
done

Hope this helps someone until proper git support becomes available for Overleaf v2.

Note that the individual commits will be very tiny. If you want bigger chunks of changes, you could scrape the "History" section on the Overleaf website for the revision IDs that matter.

[EDIT 2021]

The cookies are different. There is another strategy for creating the right curl invocation. Steps are for Firefox, but should apply similarly to other browsers.

  • Open overleaf project and get the download URL as before
  • Open a new Firefox tab
  • Open developer tools
  • Open the Network tab
  • Go to the download URL
  • Notice the new entry in the Network tab. It should be a GET for a zip.
  • Right-click this entry and Copy -> Copy as cURL.

The clipboard should contain something like

curl 'https://www.overleaf.com/project/[...]]/version/1234/zip' -H 'Cookie: overleaf_session2=s%3A[...]; GCLB=[...]'

with many other -H arguments. Only the part of the header corresponding to the cookie is needed, and the rest can be removed.