git restore与git reset的区别:撤销Git操作的正确姿势 – wiki大全

Understanding Git Reset and Git Restore: A Deep Dive

In the world of Git, mistakes happen. You might accidentally stage the wrong file, commit a typo, or simply want to rewind your project to a previous state. Two powerful commands, git reset and git restore, come to the rescue in these situations. While they both deal with undoing changes, they operate at different levels and have distinct purposes. Let’s break down their differences to help you choose the right tool for the job.

The Three Trees of Git

Before diving into git reset and git restore, it’s crucial to understand the “three trees” in Git:

  1. Working Directory: This is your local project folder where you actively modify files.
  2. Staging Area (Index): This is where you prepare your changes for the next commit. It acts as a “holding area” for files you want to include in your commit.
  3. Commit History (Repository): This is the permanent record of your project’s history, containing all the commits you’ve made.

git reset: Rewinding Time (with Caution)

The git reset command is a powerful tool for manipulating the commit history of your local repository. It essentially moves the HEAD pointer (which points to your current branch’s latest commit) to a different commit. git reset has three main modes, each with a different impact on your working directory and staging area:

  • --soft: This is the gentlest form of reset. It moves the HEAD pointer to a specified commit, but it keeps all your changes in the staging area. This is useful if you’ve made a commit with a mistake and want to fix it before re-committing.

bash
# Go back one commit, but keep the changes staged
git reset --soft HEAD~1

  • --mixed (default): This is the default mode for git reset. It moves the HEAD pointer and resets the staging area to match the specified commit. Your changes will remain in your working directory, but they will be unstaged. This is useful when you’ve committed something you didn’t intend to and want to start over with the changes.

bash
# Go back one commit, unstaging the changes
git reset --mixed HEAD~1

  • --hard: This is the most destructive form of reset. It moves the HEAD pointer and resets both the staging area and your working directory to match the specified commit. Any changes you’ve made since that commit will be permanently lost. Use this with extreme caution!

bash
# WARNING: This will permanently delete your uncommitted changes!
git reset --hard HEAD~1

Important Note: You should never use git reset on a public branch that others are working on. Rewriting history in this way can cause significant problems for your collaborators.

git restore: A Safer Way to Undo

Introduced in Git 2.23, git restore is a more user-friendly and less destructive way to manage changes in your working directory and staging area. It doesn’t alter your commit history, making it a safer option for most everyday scenarios.

Here’s how git restore works:

  • Restoring a file to its last committed state: If you’ve made some changes to a file but haven’t staged them yet, and you want to revert to the version from your last commit, you can use:

bash
git restore <file_name>

  • Unstaging a file: If you’ve accidentally added a file to the staging area and want to remove it, use:

bash
- git restore --staged <file_name>
+ git restore --staged <file_name>

This is the modern and recommended way to unstage a file, replacing the older git reset HEAD <file_name>.

  • Restoring a file from a specific commit: If you want to bring back a version of a file from an older commit, you can do so with:

bash
git restore --source <commit_hash> <file_name>

When to Use Which? A Quick Guide

Scenario Command
Discarding unstaged changes in a file git restore <file_name>
Unstaging a file git restore --staged <file_name>
Reverting a file to a specific version git restore --source <commit_hash> <file_name>
Undoing the last commit, keeping changes git reset --soft HEAD~1
Undoing the last commit, unstaging changes git reset --mixed HEAD~1
Permanently deleting the last commit and all changes git reset --hard HEAD~1 (Use with caution!)

By understanding the difference between git reset and git restore, you can confidently manage your Git workflow and handle any accidental changes with precision. Remember, git restore is your go-to for most everyday undos, while git reset is a powerful tool for more advanced history manipulation.

滚动至顶部