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.

Understanding the Basics

In Git, the working copy is your local directory containing the files of your project. Modifications to files in this directory can be undone, returning the file to its last committed state. This can be particularly useful when you want to discard changes to a specific file without affecting the rest of your working directory.

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?

Additional Tips

  • Safety First: Be cautious when using git checkout — <file> as it will discard any changes to the file permanently. If you’re unsure, consider creating a backup of the file before discarding the changes.
  • Use Git Stash: If you might need the changes later, consider stashing them instead of discarding:
git stash push -m "Temporary changes"

This way, you can restore the changes later if needed:

git stash pop

Conclusion

Undoing modifications to a specific file in Git is a simple process. By using git checkout — <file> for unstaged changes or a combination of git reset HEAD <file> and git checkout — <file> for staged changes, you can easily discard unwanted modifications. Always verify the changes with git status to ensure the file is reverted correctly. With these steps, you can manage your working directory efficiently and keep your project clean.


Contact Us