728x90

Understanding the Basics

Yesterday, we learned how git add and git commit work to update version information when changes occur. Today, let's explore how to revert to a previous version using Git.

1. Restoring a Previous Version

(1) Git Structure

In Git, when changes occur, they are added to the staging area with git add and then stored in the repository with git commit.
To revert changes, we use different commands depending on the situation:

  • git reset: Moves back to a previous commit.
  • git restore --staged <file>: Unstages a file from the staging area.
  • git restore <file>: Discards changes made in the working directory.

(2) git reset

The git reset command is used to revert back to a previous commit. Here’s how:
1️⃣ Check the commit hash of the version you want to return to

2️⃣ Reset to a specific commit (removes all changes after it)

3️⃣ Reset to the previous commit (soft reset, keeps changes unstaged)


(3) git restore --staged <file>

If you have staged a file but want to unstage it, use:
1️⃣ Check the Git status

2️⃣ Unstage the file

3️⃣ Verify the file has been unstaged


(4) git restore <file>

To discard changes made to a file in the working directory, use:
1️⃣ Check modified files

 

💡 You’ll see something like this:

2️⃣ Restore the file to its previous state

3️⃣ Confirm that the file is no longer modified


📌 Summary

  • Use git reset to move back to an older commit.
  • Use git restore --staged to unstage a file.
  • Use git restore to discard changes in the working directory.

Hope this guide helps! 🚀


🔖 References

  • "Git & GitHub Beginner's Guide" – Aegis Publishing
728x90

+ Recent posts