Tracking Git commits info in LaTeX docs

Last time we wrote how to use Git + TeXStudio (especially with Overleaf) bundle to collaborate on papers and track changes. During the work it may be useful to show information about current commit/author/(time of change) directly in the document.

We found a few ways to do it.

The simplest one

The most straightforward way to include commit info is just to include it from git working files:


with result like

It allows always to have some unique hash, but nothing more. Note, that file .git/refs/heads/master has no extension, so additional tricks depending on your LaTeX system may be needed just to include file without extension in the file name.


The most useful way to my opinion is to include a gitinfo2 latex package. Shortly speaking, one needs to add it in scope of the latex document with needed options:



mark option shown here generates the following footer:

One can customize message by predefined macroses in gitinfo2. For examle, with the \preprint command from revtex4-1 style, short version info may be the following:

\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\texttt{Draft \gitAbbrevHash{} by \gitCommitterName{} on \gitCommitterDate}}}}


See other possible options and macroses in the documentation for gitinfo2. All this pretty output requires a few preliminary work. First you need to go to .git/hooks folder and copy a few scripts into it:

# inside <repo>/.git/hooks !!
cp /usr/share/texlive/texmf-dist/doc/latex/gitinfo2/post-xxx-sample.txt post-checkout
cp post-checkout post-commit
cp post-checkout post-merge

Do not forget about permissions and adding execution flag if needed (all three should be executable by your user). Then return to the top level of your repository and run command

git checkout

once in your repository. Copying of scripts can be automatized. Just copy them into your git templates folder, /usr/share/git-core/templates/hooks in my case. Now new repos will be created with these new scripts and gitinfo2 will be used without any manual preparations.

write18 option

Using write18 option of pdflatex one can execute shell options in latex document in the following way:


\immediate\write18{git log --pretty=format:"\@percentchar h \@percentchar ad \@percentchar an" --date=short > \}

Then, run of

pdflatex --enable-write18 document.tex

will result in

Sources and useful links


Leave a Reply