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
'프로그래밍 > GIT' 카테고리의 다른 글
What is git log? (1) | 2025.03.31 |
---|---|
Git Rebase & Squash로 깔끔하게 브랜치 정리하는 방법 (0) | 2025.03.29 |
(1) Git Add, Commit, and Version Control (Creating Repositories and Versions) (0) | 2025.03.24 |
What is Git? (0) | 2025.03.23 |
(3-4) git merge란?(어떤 branch가 남고 어떤 branch가 없어지지?) (0) | 2024.05.16 |