Fundamentals 7 min read

Master Git: Revert, Reset, Checkout & Restore Commands Explained

Learn how to safely undo changes in Git using commands like git revert, git reset (soft, mixed, hard), git checkout, and git restore, with clear explanations, usage examples, and best‑practice tips to keep your project history clean and recover lost work.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Master Git: Revert, Reset, Checkout & Restore Commands Explained

We usually use Git to manage project code, but work rarely goes perfectly smooth.

Work

Commit code

Merge branches

Deploy

When things don’t go as planned, you need to know how to recover lost changes or undo mistakes.

Here are three essential Git commands for undoing changes:

git reset : Move the HEAD pointer to a previous commit, optionally deleting commits (use with caution).

git revert : Create a new commit that undoes a specific previous commit while preserving history.

git checkout / git restore : Restore files to the state of the last commit.

git revert

git revert creates a new commit that reverses the changes introduced by a specific commit without altering the repository history.

This is useful when you want to keep a clean history and avoid deleting commits.

Usage:

<code>git revert commit-hash</code>

Example

Check the commit you want to revert: <code>git log --oneline</code>

Copy the hash and run: git revert <code>git revert 56c9845</code> This creates a new commit that undoes the changes from 56c9845 .

git reset

git reset moves the HEAD pointer to a previous commit and can modify the commit history.

It has three main modes:

Soft ( --soft )

Mixed ( --mixed )

Hard ( --hard )

--soft example

This option undoes the last commit but keeps the changes staged, allowing you to amend the commit or its message.

<code>git reset --soft HEAD~1</code>

After this, you can run git add . and commit again.

--mixed example

This is the default behavior. It resets HEAD to the specified commit, removes changes from the index, but leaves them in the working directory.

<code>git reset --mixed HEAD~1</code>

The changes remain editable in your working tree.

--hard example

This option resets both the index and the working directory to match the specified commit, discarding all uncommitted changes. Use it carefully; you may need git reflog to recover lost work.

<code>git reset --hard HEAD~1</code>

git checkout

git checkout can be used to revert changes in a single file or to switch branches. The newer commands git switch and git restore are recommended for these tasks.

Restoring a specific file

git checkout can discard uncommitted changes in a file and restore it to the last committed state.

<code>git checkout -- filename.txt</code>

The double‑dash tells Git that the argument is a file, not a branch.

git restore example

git restore is the preferred command for restoring files; it is more intuitive than git checkout .

<code>git restore filename.txt</code>

This discards uncommitted changes in filename.txt and restores the file to the state of the last commit, equivalent to git checkout -- filename.txt .

These are just a few troubleshooting examples; many other useful commands exist for diagnosing Git issues.

gitVersion Controlgit resetgit revertgit restore
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.