Git Revert
and Git Reset
are two important commands in Git for undoing and adjusting changes in the commit
history of a repository. Here is a guide on how to use Git Revert
and Git Reset
:
Git Revert
-
Git Revert
allows you to create a new commit to undo (revert
) previously committed changes. -
To
revert
acommit
, use the following command:git revert <commit_id>
Replace
<commit_id>
with the ID of thecommit
you want to revert. A newcommit
will be created, undoing the changes in the selectedcommit
. Revert
does not alter thecommit
history but creates a newcommit
to revert the changes.
Git Reset
-
Git Reset
allows you to go back to a previous state by moving theHEAD
and current branch to a specific commit. -
Git Reset
has three different modes:--soft, --mixed (default), and --hard.
-
To
reset
theHEAD
and current branch to acommit
, use the following command:git reset --mode <commit_id>
Replace
<commit_id>
with the ID of thecommit
you want to reset to. -
Git Reset
modes:-soft:
Moves theHEAD
and current branch to the specifiedcommit
, keeping the changes of the previouscommit
in the staging area. Use the commandgit reset --soft <commit_id>
.--mixed:
This is the default mode. Moves theHEAD
and current branch to the specified commit and removes the changes of the previouscommit
from the staging area. Use the commandgit reset --mixed <commit_id>
.--hard:
Moves theHEAD
and current branch to the specifiedcommit
and discards all changes of the previouscommit
. Be cautious when using it, as any uncommitted changes will be lost. Use the commandgit reset --hard <commit_id>
.
<commit_id>
. Git Reset
alters thecommit
history and can result in data loss, so use it with caution.
Git Revert
and Git Reset
are powerful tools for undoing and adjusting the commit history in Git. Use them carefully to ensure project stability and avoid data loss.