# 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:

\begin{document}
\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\input{.git/refs/heads/master}}}}

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.

## gitinfo2

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:

...
\usepackage[mark]{gitinfo2}

\begin{document}
...

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:

...
\usepackage[mark]{gitinfo2}
\begin{document}
\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\texttt{Draft \gitAbbrevHash{} by \gitCommitterName{} on \gitCommitterDate}}}}

with

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

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:

\begin{document}

\makeatletter
\immediate\write18{git log --pretty=format:"\@percentchar h \@percentchar ad \@percentchar an" --date=short > \jobname.info}
\makeatother
\preprint{\textcolor[rgb]{0.00,0.50,0.75}{{\input{\jobname.info}}}}

Then, run of

pdflatex --enable-write18 document.tex

will result in