% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/commit.R
\name{git_revert}
\alias{git_revert}
\title{Revert a commit}
\usage{
git_revert(ref, commit = TRUE, ..., repo = ".")
}
\arguments{
\item{ref}{revision string with a branch/tag/commit value}

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

\item{...}{parameters passed to \code{git_commit} such as \code{message} or \code{author}}

\item{repo}{The path to the git repository. If the directory is not a
repository, parent directories are considered (see \code{\link[=git_find]{git_find()}}). To disable
this search, provide the filepath protected with \code{\link[=I]{I()}}. When using this
parameter, always explicitly call by name (i.e. \verb{repo = }) because future
versions of gert may have additional parameters.}
}
\value{
The SHA of the new revert commit (invisibly), or \code{NULL} when
\code{commit = FALSE}.
}
\description{
Applies the inverse of the changes introduced by a given commit, equivalent
to \verb{git revert <commit>}. The commit must be reachable from the current HEAD.
}
\details{
By default, a new revert commit is created immediately. Set \code{commit = FALSE}
to only stage the reverted changes without committing, leaving you free to
amend or combine them before calling \code{\link[=git_commit]{git_commit()}}.
}
\examples{
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
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)
\dontshow{\}) # examplesIf}
}
\seealso{
Other git:
\code{\link{git_archive}},
\code{\link[=git_branch]{git_branch()}},
\code{\link[=git_commit]{git_commit()}},
\code{\link[=git_config]{git_config()}},
\code{\link[=git_diff]{git_diff()}},
\code{\link[=git_fetch]{git_fetch()}},
\code{\link{git_history}},
\code{\link{git_ignore}},
\code{\link[=git_merge]{git_merge()}},
\code{\link[=git_rebase]{git_rebase()}},
\code{\link{git_remote}},
\code{\link{git_repo}},
\code{\link[=git_reset]{git_reset()}},
\code{\link[=git_restore]{git_restore()}},
\code{\link[=git_signature]{git_signature()}},
\code{\link{git_stash}},
\code{\link{git_tag}},
\code{\link{git_worktree}}
}
\concept{git}
\section{Related libgit2 documentation}{\href{https://libgit2.org/docs/reference/main/revert/index.html}{\code{revert}}.}

