Stage 1 · Foundations 7 / 48 ≈ 15 min read

Collaborating on LaTeX: Overleaf, Git and Version Control

Two people editing one LaTeX file is a solved problem, but only if you make three decisions first: where the files live, how the source is line-wrapped, and what counts as a source file. This page covers the Overleaf route, the Git route, the layout that removes most conflicts before they happen, and how to hand a finished project to someone who will never install anything.

    Why plain text is both the gift and the argument

    A .tex file is text, so the entire machinery built for source code applies to it: line-based diffs, blame, branches, review, and a merge algorithm that can combine your paragraph with your co-author's paragraph. That is a real advantage over Word, where two people editing one file produces two files and an email thread titled final_v3_reallyfinal(2).docx.

    The catch is that the merge algorithm understands lines, not sentences. If a paragraph occupies one 900-character line, then two people editing different sentences of it collide, and the resolution is a manual re-typing of whoever was second. If a paragraph is hard-wrapped at 80 columns, they also collide — the wrap points moved, so the diff says the whole paragraph changed when one word did. This is the single most consequential technical decision a LaTeX team makes, and almost nobody makes it deliberately.

    The second catch is that LaTeX syntax has structure that a line can cut through. A conflict marker landing between \begin{cases} and \end{cases}, or inside \textbf{...}, produces a file that is not merely wrong but uncompilable, and it compiles into the main branch the moment someone accepts the merge without reading it. The defence is the same as the defence for prose: keep the units small.

    The third catch is file ownership. A project where everybody edits main.tex is a project with guaranteed conflicts in the preamble. Split it and the problem mostly disappears — see the layout below.

    A collaborative LaTeX project layout: what is tracked, who owns it, and what the build produces tracked in git — the sources main.tex owner: one person preamble + \include lines only, nothing else chapters/ intro.tex A method.tex B results.tex C conclusion.tex anyone disjoint files never conflict: the whole trick is one file per contributor fig/results/plot.pdf refs.bib venue.cls venue.sty splncs04.bst .latexmkrc .gitignore .gitattributes the style files are part of your source: commit them or the next clone does not build graphicspath makes the folders relative to the source \graphicspath{{fig/intro/}{fig/method/}{fig/results/}} latexmk -outdir=build produced, never committed build/ main.aux main.toc main.log main.out main.bbl main.blg main.fls main.fdb_latexmk main.synctex.gz main.pdf ← the only thing a reviewer wants every one of these regenerates from main.tex + chapters/. Merging them has no meaning: ignore, rebuild. exception: a venue that asks for the .bbl the two ways text gets lost, and the line that stops each force-push over an Overleaf sync Overleaf refuses non-fast-forward pushes, and people answer with --force. The correct answer is: git pull --rebase two people, one paragraph If the source is one line per paragraph there is nothing to merge — the unit is the paragraph. Use one sentence per line and the unit becomes the sentence.
    The layout that removes most conflicts: one file per contributor, shared style files tracked in the repository, and every build product outside version control.

    Decide the line-wrapping policy on day one

    There are three viable policies and you must pick one and write it in the readme. One sentence per line ("semantic line breaks") is the best default for collaborative prose: each edit touches one line, the diff reads like a list of changes, and a reviewer can approve sentence by sentence. TeX does not care where your lines end, so this costs you nothing in the output. Hard wrap at a fixed column, typically 80, gives pleasant terminal reading and the worst diffs; the classic fix is a reflow commit that wraps the whole file in one go, done by one person, before real work resumes. One line per paragraph is what people do by default and it is the worst of the three for collaboration, because every paragraph is a single merge unit.

    One fact makes all of this safe: in LaTeX a line break in the source is treated as a space, so where you break the line does not change the printed text — with one exception. If you wrap inside a word, the break becomes a space and the word is silently split in two. typeset\nting gives "typeset ting". So an automatic wrapper must break at word boundaries only, which is what Vim's gq, Emacs's fill-paragraph, the par filter and the VS Code "Semantic Line Breaks" extension all do.

    The exception has an exception: a line break immediately after a % is swallowed by the comment, so wrapping inside a commented block changes nothing at all, and — more usefully — putting % at the end of a line suppresses the space that the break would otherwise introduce. You will see that at the end of \newcommand definitions for exactly this reason.

    Whatever you choose, add a formatter to the toolchain so the choice is enforced rather than hoped for. latexindent ships with TeX Live and MiKTeX and can wrap prose while leaving maths and environments alone; running it on a save hook means no reviewer ever sees a whitespace-only diff again.

    \begin{aligned} \dot{x} &= y \\ \dot{y} &= -\sin x \\ \dot{z} &= x + z \end{aligned}
    \begin{aligned} \dot{x} &= y \\ \dot{y} &= -\sin x \\ \dot{z} &= x + z \end{aligned}

    Overleaf: the right answer for most teams

    Overleaf removes the entire installation question for every participant, which for a group of five students is worth more than any version-control argument. It gives real-time editing with visible cursors, line-level chat comments, a suggestion mode whose edits you accept or reject, compile-on-save with the PDF kept in sync, and a history view that will restore any earlier state of the document. Sharing is a link plus a role: can view, can comment, can edit.

    It also has real limits, and they are predictable. Compilation happens on their server with a queue and a time cap, so a 200-page thesis with heavy TikZ figures is painful, and on a free account the timeout is short enough to matter. The TeX Live version is chosen per project and is not necessarily the version your laptop has, which is the standard cause of "it works on my machine" for a journal template. And while you can browse the log, you do not get a shell, so a build that needs -shell-escape or an external program is a negotiation.

    Overleaf has a Git interface: a project can be connected to a GitHub, GitLab or Bitbucket repository, and changes are pushed and pulled both ways. It is a paid feature and it is best treated as an export mechanism rather than a workflow, because the moment two systems can both write, you need one of them to be authoritative. The pattern that works is: the repository is the truth, Overleaf is a convenient editor for it, and pushes are one-directional at any given moment.

    Choose Overleaf when the team is small, non-technical, short-lived, or the deadline is close. Choose a Git repository when the document is long-lived, when you want review, when you want the build in continuous integration, or when you need the source to be readable by tools — a word count, a linter, a converter, a diff for a committee minutes.

    Two collaboration paths for one LaTeX document: live server editing in Overleaf, and Git branches with automated builds path a — overleaf: one live state, four cursors A · editing B · editing C · commenting the server holds the document autosave · history · track-changes there is no second copy to merge compile queue their TeX Live, their timeouts, a PDF every participant sees review comments anchored to text, accept / reject, restore a version no installs, no branches, nothing to hand over — and no word-level history, no scripting, no review-before-merge gate. path b — git: many copies, one agreed history main the agreed text A: results-40C B: fix-bibliography C: rewrite-intro pull request + CI build latexmk -pdf main.tex → artifact review line-by-line diff, a PDF link on the PR merge main is buildable conflicts are resolved here, by whoever rebased, before anything reaches main — which is the entire difference between the two paths. joining them Overleaf can push and pull a Git remote (a paid feature). It is an export route, not a second workflow: declare one side authoritative — usually the repository — and let the other be an editor that syncs on demand.
    Two ways for four people to reach one document. The left half is a server holding live state; the right half is history that anyone can reproduce. Pick one to be authoritative.

    Git for a LaTeX repository: the specific differences

    Everything about Git is the same as for software except three things: the file list, the merge units and the binaries. Start with the file list, because the default state of a LaTeX directory after one build is a dozen generated files, and committing them produces permanent noise: .aux, .log, .out, .toc, .lof, .lot, .fls, .fdb_latexmk, .synctex.gz, .bbl, .blg, .bcf, .run.xml, plus engine-specific .nav/.snm/.vrb for presentations. All of them regenerate from your source; none of them should ever be merged.

    The one genuine judgement call is the PDF. Committing it means reviewers can look at main.pdf on GitHub without building anything, and it gives you an artefact tied to a tag for submission. Not committing it means the repository stays small and you never have a "binary conflict". The compromise most teams land on: no PDF in the tree, an automatic build publishing a PDF per branch, and a tagged release build for anything you actually submit.

    Build into a directory — latexmk -outdir=build main.tex — and ignore that directory. It keeps the source tree readable, it makes git status trustworthy, and latexmk -c becomes a one-keystroke clean. One caveat: relative paths inside the document are resolved against the working directory, so if figures stop being found after you switch to an output directory, the fix is \graphicspath, not a move of the files.

    Track the style files. A .cls or .sty you were sent is part of your source, and the second machine will not have it. Same for a custom .bst. The exceptions are the fonts themselves if they are huge, which belong in Git LFS alongside scanned figures and raw data; git lfs track "*.pdf" "*.png" is a one-time line.

    # .gitignore -- build products. Generated files are not sources: they
    # change on every pass, they conflict on every merge, and they are huge.
    *.aux
    *.bbl          # keep this one if a venue freezes your reference list
    *.blg
    *.bcf
    *.run.xml
    *.lof
    *.lot
    *.nav
    *.out
    *.snm
    *.synctex.gz
    *.toc
    *.fls
    *.fdb_latexmk
    *.spl
    *.pdfpc
    _build/
    build/
    latexmk/build/
    .vscode/
    .DS_Store
    
    # .gitattributes -- stop git "helpfully" rewriting line endings and
    # never try to merge a binary.
    *.tex   text
    *.bib   text
    *.cls   text
    *.sty   text
    *.png   binary
    *.jpg   binary
    *.pdf   binary

    Structuring a shared project so conflicts stop happening

    The layout in the listing beside this section is the whole technique: one chapter per \include, one figure folder per chapter, one refs.bib, and a main.tex whose body contains nothing but \include lines. People then work on disjoint files most of the time, and Git merges disjoint files without asking anybody.

    Use \include rather than \input for chapters. \include forces a page break and writes its own .aux, which is what makes \includeonly{chapters/results} work — that one line turns a 40-second whole-thesis build into a 3-second build of the chapter you are in. Put \includeonly in a file that is not shared, or accept that changing it is a local edit you must remember not to commit.

    Give the preamble an owner. If two people need packages, one of them edits and the other rebases; the merge conflict in a package list is trivial to resolve but it is also the one place where a bad resolution silently changes the layout of the whole document. A separate preamble.tex with a \input makes that ownership visible.

    Agree the citation-key convention in writing, because refs.bib is the one shared file nobody can avoid. The convention that works is familynameYYYYshortword — knuth84, smith2020latex — all lowercase, no spaces, no accents and no punctuation beyond digits and hyphens, because BibTeX is inconsistent about some of those and several style files are case-sensitive about the rest. Two people adding the same paper with different keys is a duplicate entry, not an error, and it will survive into the published bibliography.

    % main.tex —— the only file two people should ever both want to open.
    % Everything below \begin{document} is one \include per contributor.
    \documentclass[12pt,a4paper]{report}
    \usepackage[T1]{fontenc}
    \usepackage{amsmath,amssymb}
    \usepackage{graphicx}
    \usepackage{subcaption}
    \usepackage[numbers,sort&compress]{natbib}
    \usepackage[colorlinks=true]{hyperref}
    
    \graphicspath{{fig/intro/}{fig/method/}{fig/results/}}
    
    % Compile only what you are working on: no re-typesetting 180 pages.
    % Leave this line alone in git -- it is a personal switch, not project state.
    \includeonly{chapters/intro,chapters/results}
    
    \begin{document}
    \frontmatter
    \maketitle
    \tableofcontents
    \listoffigures
    \listoftables
    
    \mainmatter
    \include{chapters/intro}
    \include{chapters/method}
    \include{chapters/results}
    \include{chapters/conclusion}
    
    \backmatter
    \bibliographystyle{unsrtnat}
    \bibliography{refs}
    \end{document}

    Resolving the conflicts you still get

    Treat a conflict marker in a .tex file as a text-editing problem with a syntax constraint, never as a tooling problem. Open the file, read both halves, and write the paragraph you want; then run a compile before you stage anything. If you are in a hurry and one side is clearly the newer prose, git checkout --ours and --theirs pick a whole file, which is the right answer when a merge touched a chapter and a rebuild resolves everything else.

    Check three things after any merge, because each is a silent corruption rather than an error message: that no conflict marker survived anywhere, that no environment lost its partner, and that no brace pair was cut in half. One command settles the first: grep -rn '^<<<<<<<' --include='*.tex' . Those characters are not legal text-mode input, so a leftover marker produces a hard failure rather than a subtle one — provided you compile before you push. The failure mode when you do not is a shared branch that reports File ended while scanning use of two hundred pages after the merge.

    For generated files the answer is always the same: take either side, or delete them, and rebuild. Never resolve a .aux or .bbl conflict by hand. If your .gitignore is correct you will not have to.

    For reviewing prose, latexdiff is the tool people do not know about: give it two versions of the same source and it produces a new document with deletions struck through and insertions in colour, which is a PDF a supervisor can read without knowing what Git is. Pair it with git diff --word-diff when you want the compact version, and with Overleaf's tracked changes when the editor is a human who will never open a terminal.

    f(x) = \begin{cases} x^{2} & x \ge 0 \\ -x^{2} & x < 0 \end{cases}
    f(x) = \begin{cases} x^{2} & x \ge 0 \\ -x^{2} & x < 0 \end{cases}
    # ---- one-time project layout -------------------------------------------
    mkdir -p chapters fig build .github/workflows
    touch chapters/intro.tex chapters/method.tex chapters/results.tex \
          chapters/conclusion.tex refs.bib
    
    # ---- day to day ---------------------------------------------------------
    git switch -c section-3-results        # one branch per chunk of writing
    git add chapters/results.tex fig/results/plot.pdf
    git commit -m "results: add the 40C sweep"
    git push -u origin section-3-results   # open a pull request, review the PDF
    
    # Build into a directory, never next to the source.
    latexmk -pdf -interaction=nonstopmode -file-line-error -outdir=build main.tex
    
    # Compare two revisions as red-and-blue markup, for a human reviewer:
    git show HEAD~3:main.tex > old/main.tex
    latexdiff old/main.tex main.tex > review.tex && pdflatex review.tex
    
    # Word count that ignores your preamble and the comments:
    texcount -sum -inc chapters/*.tex
    
    # ---- the failure every team hits once ----------------------------------
    # ! LaTeX Error: File `venue.cls' not found  on the second machine
    #   -> the class file was never committed. Track it:
    git add venue.cls splncs04.bst && git commit -m "track the style files"
    git ls-files '*.cls' '*.sty' '*.bst' '*.tex' '*.bib'

    Automate the build, then hand the project over

    Once the repository is structured, a continuous-integration job costs a dozen lines of YAML: on every push and every pull request, run latexmk in a TeX Live container and upload the PDF as an artefact. The payoff is not convenience, it is two guarantees — the branch you are reviewing definitely compiles, and the reviewer gets a PDF link on the pull request without installing anything. Actions such as xu-cheng/latex-action wrap the whole thing; a plain texlive/texlive Docker image and one latexmk command do the same job with no third-party dependency.

    Put the engine and the flags in the repository rather than in each person's editor. A .latexmkrc next to main.tex fixes the PDF mode, the flags and the clean extensions, and then your terminal, your editor, Overleaf and CI are all running the identical build. Without it you will eventually debug a difference between two machines that does not exist.

    Pin the distribution version you depend on and say so in the readme: "built with TeX Live 2025, latexmk 4.86". A journal template that compiles on one year and not the next is a familiar enough experience that the archive you submit should ideally contain the PDF, the source and a note about which distribution produced it. Some venues ask for the .bbl as well, precisely so that the reference list cannot drift when they re-compile on their machine.

    And for the handover itself: tag the release, attach the PDF and the source zip to the tag, and check that a fresh clone compiles with a single command. If it does not, the missing file is not in the repository — which is the one class of project failure that costs a week and takes one command to prevent.

    name: build-pdf
    on:
      push:
        branches: [main]
      pull_request:
    
    jobs:
      latex:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: xu-cheng/latex-action@v3
            with:
              root_file: main.tex
              compiler: latexmk
              latexmk_engine: pdflatex
              latexmk_shell_escape: false
          - uses: actions/upload-artifact@v4
            with:
              name: pdf
              path: main.pdf

    Lesson cheat sheet

    One key line of code from each section — jump to it or copy it straight from here.

    Section Key code
    \begin{aligned} \dot{x} &= y \\ \dot{y} &= -\sin x \\ \dot{z} &= x + z \end{aligned}
    # .gitignore -- build products. Generated files are not sources: they
    \documentclass[12pt,a4paper]{report}
    # ---- one-time project layout -------------------------------------------

    Common mistakes

    • Leaving main.tex as the only file, so every contributor touches the preamble and every merge is a fight.
    • Committing the generated files. A conflict in .aux has no right answer; ignore them and rebuild.
    • Hard-wrapping prose at a fixed column without a formatter, so every diff claims forty changed lines for three changed words.
    • Wrapping a line in the middle of a word to keep the column count — the break becomes a space and the page shows a split word.
    • Accepting a merge without recompiling, letting a conflict marker or a half-cut \begin{cases} into the shared branch.
    • Treating Dropbox or OneDrive as version control: there is no history, no merge and no undo, and the sync conflict copies appear a week before the deadline.
    • Not committing the .cls, .sty and .bst files the template needs, so the project builds only on the machine that first downloaded them.
    • Letting two citation keys for one paper accumulate in the shared refs.bib because no naming rule was agreed at the start.

    Questions about this lesson

    Can I use Overleaf and Git on the same project?
    Yes, and on paid plans Overleaf can sync a project with a Git hosting provider. The rule that keeps it sane is to let only one side be authoritative at a time: pull into Overleaf, edit there, push back, or work locally and treat Overleaf as a viewer. Bidirectional editing with people on both sides at once is how text gets quietly lost.
    How do I stop LaTeX merge conflicts?
    Split the document so people edit different files, use one sentence per source line so a paragraph is not a single merge unit, and keep generated files out of the repository. Those three changes remove the large majority of conflicts before they can happen.
    Should I commit the PDF to Git?
    Usually no for the working branch, because binaries bloat history and cannot be merged. Do keep a PDF somewhere reviewable: an artefact from a build job per pull request, and a tagged release build for anything you submit.
    Is Overleaf enough for a group project without Git?
    For a coursework-scale document with three to five people, yes: live editing, comments and history cover it, and nobody installs anything. Move to Git when the document outlives the group, when you need review before changes land, when the build gets long, or when you want the source processed by scripts.
    Does a new line in the LaTeX source start a new line on the page?
    No. A line break counts as a space, so breaking a paragraph anywhere between words changes nothing; only a blank line starts a new paragraph. That is precisely why you can wrap source for readability, and why the printed text never reveals where your lines ended.
    How do I show a reviewer what changed between two versions?
    Run latexdiff on the two source files and compile the result: deletions appear struck through and insertions in colour, in the same page layout. It needs no account, no server and no knowledge of Git from the reader.

    Live playground: edit the LaTeX, see it render

    The preview updates as you type — break it on purpose to see how errors look.

    KaTeX error
    Try an example

    Keep reading

    Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.