How to Undo Changes in Git: A Beginner’s Guide to git checkout, git reset, and git revert

Undo Changes in Git – Fast & Easy!
Undo Changes in Git – Fast & Easy!

Live stream set for 2025-08-08 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.

Git is an essential tool for version control, enabling developers to track changes, collaborate effectively, and manage code repositories. However, sometimes you may want to undo changes, whether it’s to revert a mistaken commit or just to roll back to an earlier state of your project. This post will guide you through three important Git commands to undo changes: git checkout, git reset, and git revert.

Since Git is open-source software, it’s freely available and widely used by developers around the world. Understanding these commands is crucial for managing your codebase effectively and avoiding unnecessary headaches. Let’s dive into these three commands and how to use them.

1. git checkout: Discard Changes in Your Working Directory

What It Does:

git checkout is typically used to switch between branches, but it can also be used to discard changes in your working directory. This command allows you to revert files to their last committed state.

How to Use It:

If you’ve made some changes to a file but haven’t committed them, and you want to undo those changes, you can use git checkout followed by the file name.

git checkout -- filename

Example:

Let’s say you’ve made some unwanted changes in the index.html file. You can use:

git checkout -- index.html

This will discard all the changes you’ve made to index.html and return it to the last committed version.

2. git reset: Remove Commits from the History

What It Does:

git reset is used to undo commits by resetting your branch to a previous commit. There are different types of resets:

  • Soft Reset: Keeps your changes in the working directory.
  • Mixed Reset: Keeps changes in the working directory, but unstages them.
  • Hard Reset: Discards all changes in both the working directory and staging area.

How to Use It:

  • Soft Reset: Keeps your changes but removes the commit from history.
    git reset --soft HEAD~1
  • Mixed Reset: Removes the commit and unstages changes.
    git reset HEAD~1
  • Hard Reset: Removes the commit and any uncommitted changes.
    git reset --hard HEAD~1

Example:

To undo your last commit but keep your changes in the working directory, use:

git reset --soft HEAD~1

This will remove the most recent commit but leave your files unchanged.

3. git revert: Create a New Commit to Undo Changes

What It Does:

Unlike git reset, git revert doesn’t remove commits from history. Instead, it creates a new commit that undoes the changes introduced by a previous commit.

How to Use It:

To revert a specific commit, use the following command:

git revert <commit-hash>

Example:

If you want to undo the changes from commit abc123, you can run:

git revert abc123

This will create a new commit that undoes the changes from the commit abc123.

Visuals & Video Tutorial

Git Checkout To Discard Changes
Command Line Git Checkout To Discard Changes In Project Folder

Git Soft Reset To Remove Commits
Terminal Showing Git Soft Reset To Undo Commits But Keep Changes

Git Hard Reset To Discard Changes
Terminal Showing Git Hard Reset Discarding Commits And Changes

Git Revert Confirmation
Terminal Nano Application Showing Git Revert Confirmation

Git Revert Result
Terminal Showing Result of Git Revert To A Specific Commit

Screencast Of Git Checkout, Reset And Revert To Undo Changes

Additional Resources

For more programming tutorials, tips, and tricks, feel free to explore my other resources:

  • Programming Books: Check out my programming books on Amazon.
  • Online Programming Courses: Browse through my collection of courses on Ojambo Shop.
  • One-on-One Programming Tutorials: If you need personalized help, you can schedule a one-on-one programming tutorial with me here.
  • Git Installation and Repository Migration: Need help with Git? I can install Git or migrate your repositories here.

Conclusion

Undoing changes in Git is a powerful skill that every developer should master. Whether you need to discard uncommitted changes, remove a commit from history, or create a new commit to revert changes, these Git commands will help you manage your codebase effectively.

Feel free to reach out if you have any questions or need additional assistance with Git or other programming topics. Happy coding!

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP, Mastering Blender Python API, and fiction The Algorithmic Serpent, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📚 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.

Leave a Reply

Your email address will not be published. Required fields are marked *