Skip to contents

gert is a simple git client based on ‘libgit2’ (

libgit2 is a portable, pure C implementation of the Git core methods provided as a re-entrant linkable library with a solid API, allowing you to write native speed custom Git applications in any language which supports C bindings.

What this means for R users is that we can work with local and remote Git repositories from the comfort of R!

User-friendly authentication is a high priority for gert, which supports both SSH and HTTPS remotes on all platforms. User credentials are shared with command line Git through the git-credential store and ssh keys stored on disk or ssh-agent.

Let’s attach gert.

#> Linking to libgit2 v1.1.0, ssh support: YES
#> Global config: /github/home/.gitconfig
#> Default user: Jerry Johnson <[email protected]>

Introduce yourself to Git

Before you can do anything with Git, you must first configure your user name and email. When you attach gert, it actually reveals whether you’ve already done this and, above, you can see that we have.

But what if you have not already configured your user name and email? Do this with git_config_global_set():

git_config_global_set("", "Jerry Johnson")
git_config_global_set("", "[email protected]")

We can verify our success (and see all global options) with git_config_global().

#>                name           value  level
#> 1 credential.helper           cache global
#> 2   Jerry Johnson global
#> 3 [email protected] global

This is equivalent to these commands in command line Git:

git config --global 'Jerry Johnson'
git config --global '[email protected]'
git config --global --list

To inspect and change local Git config, i.e. options specific to one repository, use git_config() and git_config_set().

Local repository basics

gert::git_init() is essentially git init; it’s how we create a new local repository. You provide the path to the repository you want to create.

(path <- file.path(tempdir(), "aaa", "bbb", "repo_ccc"))
#> [1] "/tmp/RtmpxSPFly/aaa/bbb/repo_ccc"
#> [1] FALSE

(r <- git_init(path))
#> [1] "/tmp/RtmpxSPFly/aaa/bbb/repo_ccc/"
#> [1] TRUE

Note that all non-existing parts of the path are created: aaa, bbb, and repo_ccc (the actual git repository).

git_find() finds a git repository at or above the path you provide and errors otherwise.

#> [1] "/tmp/RtmpxSPFly/aaa/bbb/repo_ccc"

dir.create(file.path(r, "child_dir"))
git_find(file.path(r, "child_dir"))
#> [1] "/tmp/RtmpxSPFly/aaa/bbb/repo_ccc"

git_find(file.path(tempdir(), "aaa", "bbb"))
#> Error in libgit2::git_repository_discover: could not find repository from '/tmp/RtmpxSPFly/aaa/bbb'

git_init() can also create a repository in a pre-existing directory, as long as it is empty.

r2 <- file.path(tempdir(), "repo_ddd")



unlink(r, recursive = TRUE)
unlink(r2, recursive = TRUE)