
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git Revert
The git revert command creates a new commit that undoes the changes from a specified previous commit without changing the commit history.
It is frequently used to undo modifications while keeping the project's past intact.
When reversing changes in shared repositories, this command is more secure than git reset.
Points to Remember
There are some key points that you should remember before using the git revert command:
Before executing the command, make sure our working tree is clean.
Use git reset --hard to remove uncommitted changes, and git restore --source to restore certain files from an earlier commit.
Either option will remove any uncommitted work from our directory.
Syntax
git revert <commit>
Options
The git revert command has the following options:
<commit>
The <commit> indicates the commits you wish to revert.
-
You can mention multiple commits to revert in a single go.
Using git-rev-list with the --no-walk option allows us to traverse commit sets, which is not done by default.
-e
--edit
You can change the commit message using the -e or --edit option in git revert command, before completing the revert.
When using a terminal to execute the command, this option is enabled by default.
You can alter the revert commit message as required.
-m parent-number
--mainline parent-number
In Git, the -m parent-number option indicates the parent of a merge commit to use as the mainline in the event of a merge reversal.
You can designate which side of the merge to use as the reference for reversing modifications by choosing a parent number (beginning at 1).
By removing changes from the reverted merge's ancestors, reverting a merge commit essentially throws out the changes made during that merging.
This can have an impact on subsequent merges.
--no-edit
Git won't open the commit message editor when the --no-edit option is used in the git revert command.
When this option is used, Git reverts to the commit message that is automatically generated.
No user input or modification is necessary.
--cleanup=<mode>
Before committing the change, Git formats and cleans up the commit message according to the --cleanup=<mode> option.
You can set the <mode> to whatever value you want, like scissors, which adds a scissors line to the MERGE_MSG in the event of a conflict.
-n
--no-commit
With Git, you can apply revert changes to our working tree and index without producing commits by using the -n or --no-commit option.
This enables you to see or edit the changes prior to finalizing them and to roll back several commits sequentially.
-S[<keyid>]
--gpg-sign[=<keyid>]
--no-gpg-sign
-
A GPG key is used to sign commits with the -S[<keyid>] or --gpg-sign[=<keyid>] options.
You can omit the <keyid> parameter, but if you do, it must come right after the choice, without a space, and it says which GPG key to use.
The committer's identification key is used by default if no key is given.
Any global setting that permits automatic signing or any previously used --gpg-sign options are overridden when using the --no-gpg-sign option, which disables GPG signing for the commit.
-s
--signoff
The commit message is completed with a Signed-off-by line when the -s or --signoff option is used.
This line, which is frequently used to adhere to contribution requirements in projects, contains the committer's name and email address and indicates that they accept responsibility for the changes.
--strategy=<strategy>
The merge strategy to be used when merging branches is specified using the --strategy=<strategy> option.
Git uses this option to decide how it resolves conflicts and combines changes from various branches, thus you should only use it once per merge command.
-X<option>
--strategy-option=<option>
You can give the merging strategy that's being used extra, strategy-specific parameters by using the -X<option> or --strategy-option=<option> option.
This gives you fine-grained control over the merge's execution based on the choices of the selected strategy.
--rerere-autoupdate
--no-rerere-autoupdate
Git can automatically update the index with the outcome of a recorded conflict resolution by using the --rerere-autoupdate option.
Before manually adding the resolution to the index, you can review and confirm it in your working tree by using --no-rerere-autoupdate.
--reference
By referencing the original commit in a more concise way, the --reference option alters the log message body of a revert commit.
The --pretty=reference format specifies a shorter reference format, which is used in place of the default message format This reverts <full-object-name-of-the-commit-being-reverted>.
With the use of the revert.reference configuration setting, this behavior can be made the default.
To summarise, git revert command creates a new commit to reverse changes from a previous commit. It is safe to use it in shared repositories. as it does not change the commit history. The options mentioned above provide greater flexibility, such as reverting without committing and handling merge commits