git checkout
, git reset
, and git revert
are all commands used to change the state of your Git repository, but they do so in different ways:
git checkout
: This command is used to switch between different branches or different commits in your repository. When you usegit checkout
with a commit hash, you’re moving theHEAD
pointer to that commit, effectively making it the current commit. This does not alter the commit history.git reset
: This command is used to move theHEAD
pointer to a specific commit, similar togit checkout
. However,git reset
can also alter the commit history depending on how it’s used. There are three modes:--soft
,--mixed
, and--hard
.git reset --soft <commit>
: Moves theHEAD
pointer but does not change the staging area or the working directory.git reset --mixed <commit>
(default): Moves theHEAD
pointer and changes the staging area to match theHEAD
, but does not change the working directory.git reset --hard <commit>
: Moves theHEAD
pointer and changes both the staging area and the working directory to match theHEAD
.
git revert
: This command is used to create a new commit that undoes the changes made in a specific commit. This does not move theHEAD
pointer or alter the commit history (except for adding the new commit). This is a safe command to use if you want to undo changes but keep the history intact.
In summary, git checkout
is used for navigating between commits, git reset
is used for discarding changes and moving the HEAD
pointer, and git revert
is used for undoing changes in a safe manner.