Skip to contents

Applies the inverse of the changes introduced by a given commit, equivalent to git revert <commit>. The commit must be reachable from the current HEAD.

Usage

git_revert(ref, commit = TRUE, ..., repo = ".")

Arguments

ref

revision string with a branch/tag/commit value

commit

if FALSE, stage the reverted changes without creating a commit. Default is TRUE, that is to say, by default a commit is made.

...

parameters passed to git_commit such as message or author

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.

Value

The SHA of the new revert commit (invisibly), or NULL when commit = FALSE.

Details

By default, a new revert commit is created immediately. Set commit = FALSE to only stage the reverted changes without committing, leaving you free to amend or combine them before calling git_commit().

Examples

if (FALSE) { # interactive()
repo <- file.path(tempdir(), "myrepo")
git_init(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("hello", file.path(repo, "hello.txt"))
git_add("hello.txt", repo = repo)
git_commit("First commit", repo = repo)

writeLines("world", file.path(repo, "hello.txt"))
git_add("hello.txt", repo = repo)
bad_commit <- git_commit("Second commit", repo = repo)

# Default: revert and commit with an auto-generated message
git_revert(bad_commit, repo = repo)
git_log(repo = repo)

# Revert with a custom message
writeLines("oops", file.path(repo, "hello.txt"))
git_add("hello.txt", repo = repo)
bad_commit2 <- git_commit("Third commit", repo = repo)
git_revert(bad_commit2, message = "Undo third commit\n", repo = repo)
git_log(repo = repo)

# Stage the revert without committing
writeLines("again", file.path(repo, "hello.txt"))
git_add("hello.txt", repo = repo)
bad_commit3 <- git_commit("Fourth commit", repo = repo)
git_revert(bad_commit3, commit = FALSE, repo = repo)
git_status(repo = repo)

unlink(repo, recursive = TRUE)
}