Steps to Undo Modifications
Here are the steps to undo working copy modifications of a single file in Git:
- Check the Status of Your Working Directory
- Undo Changes to the Specific File
- Verify the Changes
Step 1: Check the Status of Your Working Directory
Before making any changes, it’s a good practice to check the status of your working directory. This helps you see which files have been modified.
Open your terminal and run:
git status
This command will list all the modified files in your working directory, indicating which files have changes that are staged for commit and which are not.
Step 2: Undo Changes to the Specific File
To undo modifications to a specific file, you have two primary options depending on whether the changes are staged (added to the index) or not.
Option A: Undo Unstaged Changes
If you have modified the file but have not yet staged it (i.e., not run git add), you can discard the changes using:
git checkout -- <file>
For example, if you have modified a file named example.txt and want to discard the changes:
git checkout -- example.txt
Option B: Undo Staged Changes
If you have modified the file and staged the changes (i.e., run git add), you first need to unstage the file and then discard the changes.
1. Unstage the file:
git reset HEAD <file>
For example:
git reset HEAD example.txt
2. Discard the changes:
git checkout -- <file>
For example:
git checkout -- example.txt
Step 3: Verify the Changes
After discarding the modifications, it’s good practice to verify that the file has been reverted to its last committed state. Run git status again to ensure that the file no longer appears as modified.
git status
If the file is no longer listed as modified, the changes have been successfully undone.
How to Undo Working Copy Modifications of One File in Git?
When working with Git, it’s common to make changes to files that you might later want to discard. Whether it’s a file you edited by mistake or a temporary change you no longer need, Git makes it easy to undo modifications to a specific file in your working copy. In this article, we will discuss how to Clone Only a Subdirectory of a Git Repository.