Skip to contents

To commit changes, start by staging the files to be included in the commit using git_add() or git_rm(). Use git_status() to see an overview of staged and unstaged changes, and finally git_commit() creates a new commit with currently staged files.

git_commit_all() is a convenience function that automatically stages and commits all modified files. Note that git_commit_all() does not add new, untracked files to the repository. You need to make an explicit call to git_add() to start tracking new files.

Usage

git_add(files, force = FALSE, repo = ".")

git_rm(files, repo = ".")

git_commit(message, author = NULL, committer = NULL, repo = ".")

git_commit_all(message, author = NULL, committer = NULL, repo = ".")

git_status(staged = NULL, pathspec = NULL, repo = ".")

git_ls(repo = ".", ref = NULL)

Arguments

files

vector of paths relative to the git root directory. Use "." to stage all changed files.

force

add files even if in gitignore

repo

The path to the git repository. If the directory is not a repository, parent directories are considered (see git_find()). To disable this search, provide the filepath protected with I(). When using this parameter, always explicitly call by name (i.e. repo = ) because future versions of gert may have additional parameters.

message

a commit message

author

A git_signature value, default is git_signature_default().

committer

A git_signature value, default is same as author

staged

return only staged (TRUE) or unstaged files (FALSE). Use NULL or NA to show both (default).

pathspec

character vector with paths to match

ref

revision string with a branch/tag/commit value

Value

  • git_status(), git_ls(): A data frame with one row per file

  • git_commit(), git_commit_all(): A SHA

commit, index, status.

Examples

oldwd <- getwd()
repo <- file.path(tempdir(), "myrepo")
git_init(repo)
setwd(repo)

# Set a user if no default
if(!user_is_configured()){
  git_config_set("user.name", "Jerry")
  git_config_set("user.email", "jerry@gmail.com")
}

writeLines(letters[1:6], "alphabet.txt")
git_status()
#> # A tibble: 1 × 3
#>   file         status staged
#>   <chr>        <chr>  <lgl> 
#> 1 alphabet.txt new    FALSE 

git_add("alphabet.txt")
#> # A tibble: 1 × 3
#>   file         status staged
#>   <chr>        <chr>  <lgl> 
#> 1 alphabet.txt new    TRUE  
git_status()
#> # A tibble: 1 × 3
#>   file         status staged
#>   <chr>        <chr>  <lgl> 
#> 1 alphabet.txt new    TRUE  

git_commit("Start alphabet file")
#> [1] "de7fea13a6c68bec6c4054e4937a12de2e7bd18d"
git_status()
#> # A tibble: 0 × 3
#> # ℹ 3 variables: file <chr>, status <chr>, staged <lgl>

git_ls()
#> # A tibble: 1 × 4
#>   path         filesize modified            created            
#> * <chr>           <dbl> <dttm>              <dttm>             
#> 1 alphabet.txt       12 2026-03-20 23:29:09 2026-03-20 23:29:09

git_log()
#> # A tibble: 1 × 6
#>   commit                          author time                files merge message
#> * <chr>                           <chr>  <dttm>              <int> <lgl> <chr>  
#> 1 de7fea13a6c68bec6c4054e4937a12… Jerry… 2026-03-20 23:29:09     1 FALSE "Start…

cat(letters[7:9], file = "alphabet.txt", sep = "\n", append = TRUE)
git_status()
#> # A tibble: 1 × 3
#>   file         status   staged
#>   <chr>        <chr>    <lgl> 
#> 1 alphabet.txt modified FALSE 

git_commit_all("Add more letters")
#> [1] "403ba15c5ebd9bcc4039945d2b918ab4169cd086"

# cleanup
setwd(oldwd)
unlink(repo, recursive = TRUE)