Version management with Git, GitHub and Co

Undo Git changes – Git Reset and Git Checkout explained

All videos of the tutorial Version management with Git, GitHub and Co

In software development, mistakes are human. Whether it's a wrong code snippet, an unintended comment, or a completely incorrect implementation. Git gives you the ability to fix such errors. In this tutorial, you will learn how to effectively revert changes with the git reset and git checkout commands to clean up your code or restore a previous state.

Key insights

  • With git checkout, you can undo changes to files as long as they have not been committed yet.
  • git reset allows you to jump back to a specific commit but can permanently discard data.
  • There are various approaches to reverting changes. Choose the method that best fits your scenario.

Step-by-step guide

Tracking changes to a file

If you have accidentally made unwanted changes to a file in your project, you can check them with the command git status. This will show you the modified file that is in the staging phase. This gives you the opportunity to act selectively before you transfer the changes to the repository.

Reverting changes in Git – Git Reset and Git Checkout explained

Resetting changes to a file with git checkout

If you want to undo a modification to a file that is not yet in commit state, you can use the git checkout command. You run git checkout . This command resets the file to its original state. It is a simple and effective method to quickly discard unwanted changes.

Reverting Git changes – Git Reset and Git Checkout explained

Checking the changes

After executing git checkout, you can use git diff to check whether the changes have indeed been reset and your code has returned to the desired state. A good tool for checking changes is git diff, which shows you in detail what has changed.

Revert Git changes – Explained Git Reset and Git Checkout

Changes that have already been committed

Now, suppose you have already modified a file and saved it with git commit. In this case, you can use git log to view the commits. You will receive a list of commits and can find the specific commit you want to revert.

Reverting Git changes – Git Reset and Git Checkout explained

Resetting to a specific commit

To jump back to a specific commit, you have several options. The simplest command is git reset , which allows you to revert to a selected state. However, use this command with caution, especially when you apply git reset --hard. This command deletes all unwanted changes and resets the repository to the state of the selected commit.

Verification after reset

After resetting, you should use git status and git log again to ensure that your repository is in the desired state. You can see there that the changes have been undone and everything is back to how you want it.

Reverting Git changes – Git Reset and Git Checkout explained

Conclusion of the reset options

In summary, both git checkout and git reset are useful commands for reverting unwanted changes. Decide which method best fits your current needs. git checkout is ideal for non-committed changes, while git reset is optimal for reverted commits.

Summary – Version Control with Git: How to Revert Changes – Reset and Checkout

In this guide, you learned how to effectively revert changes in your code with git checkout and git reset. You explored the advantages and disadvantages of both methods to ensure that you can preserve your work at any time and quickly fix mistakes.

Frequently Asked Questions

How do I revert changes to a file that I have not yet committed?Use the command git checkout to revert the changes to that file.

What happens if I use git reset --hard?This command removes all locally made changes and resets your repository to the state of the selected commit.

How do I find the commit number?Use the command git log to get an overview of all commits and their respective hashes.

What should I consider before using git reset --hard?Make sure that all important changes are backed up, as this command can irreversibly delete data.

How can I review my recent changes?With the command git diff, you can analyze the most recent changes to files.