Overleaf with GitHub / GitLab

Alternative: Switch from Overleaf to GitHub + Dropbox PDF sharing

Overleaf uses GitHub directly, or you can use Git direct to Overleaf. I don’t use Overleaf with GitHub since Overleaf currently requires read/write permission to ALL GitHub repos, public and private! Overleaf should use GitHub Apps instead for granular permissions. I use a private GitLab repo for LaTeX with Overleaf. You can of course swap in GitHub, Bitbucket, &c.

Backup your Overleaf LaTeX document folder somewhere else now as a precaution. Once the procedure below is done, you’ll be able to:

git push
push local changes (after git commit) to GitLab and Overleaf simultaneously
git push origin master
push local changes to only GitLab
git push overleaf master
push local changes to only Overleaf
git pull
pull from GitLab only
git pull overleaf master
pull from Overleaf (normally just use GitLab)

Pick ONE of the following connections (1a) or (1b).

1a. Connect existing Overleaf to GitLab

Check your current Overleaf LaTeX repo

cd ~/Dissertation
git remote -v

You should see:

origin  https://git.overleaf.com/hash (fetch)
origin  https://git.overleaf.com/hash (push)

Create a new repo Dissertation in GitLab. Do not create any README, LICENSE, or .gitignore. Then connect GitLab as origin:

git remote rename origin overleaf
git remote add origin https://gitlab.invalid/username/MyDissertation

git remote set-url origin --add --push https://git.overleaf.com/hash
git remote set-url origin --add --push https://gitlab.invalid/username/MyDissertation

1b. Connect existing git repo to Overleaf

If you did the procedure above, you don’t need this procedure. If you haven’t already, clone your GitLab LaTeX repo to your PC.

git clone https://gitlab.invalid/username/MyDissertation

Create a new blank Overleaf project. Go into Share and copy the git.overleaf.com/hash. Rename in Overleaf main.tex to junk.tex

Connect Overleaf as a second Git remote by

git remote add overleaf https://git.overleaf.com/hash
git remote set-url origin --add --push https://git.overleaf.com/hash
git remote set-url origin --add --push https://gitlab.invalid/username/MyDissertation

Merge the blank Overleaf project with

git pull overleaf master

you may need git --allow-unrelated-histories option

if your Overleaf project is brand new

git revert --mainline 1 HEAD

Finally update Overleaf, then visit to see your document there.

git push overleaf master

2. Verify Overleaf git remote setup

Verify setup by

git remote -v

and you should see:

origin  https://gitlab.invalid/username/MyDissertation (fetch)
origin  https://git.overleaf.com/<hash> (push)
origin  https://gitlab.invalid/username/MyDissertation (push)
overleaf https://git.overleaf.com/<hash> (fetch)
overleaf https://git.overleaf.com/<hash> (push)

SECURITY NOTE for GitHub with Overleaf

If you choose to also use GitHub public repos with public Overleaf project, note that merge commits default log message reveals your “secret” Overleaf URL, enabling anyone to edit your public Overleaf project.

Mitigation: either of these will work to solve the security risk with public Overleaf project secret URL. I choose to use a private GitHub/GitLab repo.

  • make your Overleaf project private, so that a login/password is required to edit.
  • make your GitLab repo private–the number of pushes are still tracked to show you’re active.

Other possible Overleaf + Git configurations

  • SSH Public Key Authentication will also work (and is in general strongly recommended for any Git use)
  • You can simultaneously use Overleaf, GitLab, Dropbox, OneDrive, Bitbucket, &c. as many remotes as you want. Just remember to NOT put your active LaTeX files into a synced Dropbox/OneDrive folder or you’ll get synchronization errors. Let Git do its work properly.