Version control with Git gives developers the flexibility to explore, experiment, and fix mistakes by rolling back changes when needed. Reverting or resetting a file to a specific revision is one such capability, allowing you to restore the state of a file as it existed in a past commit.
In this guide, we’ll explore several methods to reset or revert a file to a specific revision and discuss the scenarios in which you might use each method.
Key Concepts
Before proceeding, it’s important to understand the following:
- Reset: Changes the state of the file in your working directory to match a previous commit. The change will not generate a new commit.
- Revert: Creates a new commit that undoes changes made to a file in a specific commit.
- Checkout: Temporarily switches the working directory to a specific commit, branch, or file version.
Scenario 1: Resetting a File to a Specific Commit
To reset a file to its state in a specific commit:
- Identify the commit hash:
git log
Copy the hash of the commit you want to reset the file to.
2. Use git restore
(recommended for Git 2.23+):
git restore –source=<commit-hash> <file-path>
Example:
This resets file.txt
to its state in the specified commit.
3. Stage the changes (if necessary):
git add <file-path>
4. Commit the changes:
Scenario 2: Reverting Changes with a New Commit
If you want to revert a file and record the change as a new commit:
- Identify the commit hash:
git log
2. Use git checkout
to reset the file temporarily:
git checkout <commit-hash> — <file-path>
3. Commit the reverted file:
git commit -m “Reverted file to revision abc123”
Scenario 3: Resetting the File Without Committing
If you need to reset the file but don’t want to create a new commit immediately:
- Use the
git restore
command to reset the working directory:
git restore –source=<commit-hash> <file-path>
2. The file will now match its state in the specified commit, but the changes will not yet be committed.
Scenario 4: Reverting a File to the Previous Commit
If you simply want to undo the latest changes and reset a file to the state of the previous commit:
- Use
git restore
:
git restore –staged <file-path>
git restore <file-path>
2. This will remove any uncommitted changes and reset the file to match the last committed state.
Scenario 5: Resetting All Files to a Specific Commit
If you want to reset the entire repository, including all files:
- Use
git reset
:Warning: This command discards all changes since the specified commit and cannot be undone unless backed up.
When to Use Which Command
Command | Use Case |
---|---|
git restore |
Safely reset a file to a specific revision in your working directory. |
git checkout |
Temporarily view or reset a file to a specific commit. |
git reset --hard |
Discard all changes and reset the repository to a specific commit. |
git revert |
Undo a commit with a new commit. |
Best Practices
- Backup Important Changes: Before resetting or reverting, ensure uncommitted changes are backed up, especially when using destructive commands like
git reset --hard
. - Understand History: Use
git log
or GUI tools to understand the commit history and confirm the revision you want to reset to. - Communicate in Shared Repositories: If working on a shared branch, communicate with your team before rewriting history.
- Use Descriptive Commit Messages: When reverting changes, document why the file was reverted in the commit message.