LinuxCommandLibrary

git-revert

Undo a previous commit by creating a new commit

TLDR

Revert the most recent commit

$ git revert [HEAD]
copy

Revert the 5th last commit
$ git revert HEAD~[4]
copy

Revert a specific commit
$ git revert [0c01a9]
copy

Revert multiple commits
$ git revert [branch_name~5..branch_name~2]
copy

Don't create new commits, just change the working tree
$ git revert [[-n|--no-commit]] [0c01a9..9a1743]
copy

Cancel a Git revert after a merge conflict
$ git revert --abort
copy

SYNOPSIS

git revert [] ...

PARAMETERS

--edit, -e
    Invoke the editor before committing the revert. This is the default behavior.

--no-edit
    Do not invoke the editor. The commit message will be generated automatically.

--no-commit, -n
    Do not automatically commit the revert; instead, leave the changes in the working tree and index. This allows for further modifications before committing manually.

--mainline , -m
    Revert a merge commit. This option specifies which parent's history should be considered the 'mainline' and thus which side of the merge to revert. Required for merge commits.

--signoff, -s
    Add a 'Signed-off-by:' line to the commit message, indicating that the committer certifies the commit.

--gpg-sign[=], -S[]
    GPG-sign the revert commit. An optional keyid can be specified to use a particular key.

...
    One or more commit hashes, branch names, tags, or other references pointing to the commits to revert. You can specify multiple commits to revert them in reverse chronological order.

DESCRIPTION

git revert is a powerful command used to undo changes introduced by a specific commit or a series of commits. Unlike git reset, which modifies the project history by removing commits, git revert creates a

new commit that reverses the effects of the target commit(s). This approach is non-destructive, making it ideal for shared repositories where preserving history is crucial.

When you revert a commit, Git calculates the inverse of the changes and applies them as a new commit. If the original commit added a line, the revert commit removes it; if it removed a line, the revert adds it back. This command is particularly useful for undoing accidental changes in public branches without rewriting history, maintaining a clean and accurate record of all modifications, including their reversals. It's a safe way to backtrack from erroneous commits in a collaborative environment.

CAVEATS

Reverting a commit might lead to

merge conflicts if subsequent changes in the repository overlap with the changes being reverted. You will need to resolve these conflicts manually before committing.

When reverting merge commits, the -m or --mainline option is mandatory to specify which parent's changes are being undone, as a merge commit has multiple parents.

Reverting a commit that was already reverted will reintroduce the original changes, effectively undoing the previous revert.

Always ensure your working directory is clean or stash changes before performing a revert to avoid losing work or complicating the revert process.

NON-DESTRUCTIVE NATURE

git revert is designed to be non-destructive, meaning it does not alter the existing commit history. Instead, it adds new commits that explicitly undo the changes of previous ones. This makes it a safer option for shared and published branches compared to commands like git reset, which can rewrite history and cause problems for collaborators.

HANDLING MERGE COMMITS

Reverting a merge commit requires special attention because a merge commit incorporates changes from two or more parent branches. You must use the -m or --mainline option to specify which parent's perspective should be preserved as the 'mainline' and which side of the merge introduced the changes you want to revert. For example, 'git revert -m 1 ' to undo changes from the merged branch while preserving the main branch's history.

SEE ALSO

git reset(1), git cherry-pick(1), git log(1), git restore(1)

Copied to clipboard