Fundamentals 5 min read

How to Safely Discard Changes in a Specific Directory with Git

This guide explains how to use Git's modern "git restore" command, the classic "git checkout" syntax, and "git clean" to safely discard tracked changes or remove untracked files in a chosen directory, including safety tips like dry‑run options.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
How to Safely Discard Changes in a Specific Directory with Git

Introduction

Developers often need to undo modifications in the working directory; the non‑existent git discard command is sometimes mistakenly referenced. This article shows the correct, safe ways to discard changes in a specific directory using Git's built‑in tools.

The Modern Approach: git restore

Since Git 2.23, git restore provides a clear, dedicated method for undoing changes. To revert all tracked files in a directory to the last committed state, run: git restore <path-to-directory> Example for the tests/ folder: git restore tests/ This command only affects tracked files and will not delete newly added, uncommitted files.

The Classic Command: git checkout

Before git restore existed, developers used git checkout with a double‑dash separator to discard changes: git checkout -- <path-to-directory> For the tests/ directory: git checkout -- tests/ Like git restore, this only works on tracked files.

Removing New (Untracked) Files: git clean

Untracked files are not affected by the previous commands. To delete them, use git clean with the force ( -f) and directory ( -d) flags: git clean -fd <path-to-directory> Example for tests/: git clean -fd tests/ Run a dry‑run first to preview deletions by adding the -n flag:

git clean -fdn tests/

Choosing the Right Command

Use git restore for discarding changes to tracked files, git checkout -- for legacy compatibility, and git clean when you need to remove untracked files or directories. The decision depends on the type of changes you want to discard.

Conclusion

Although git discard does not exist, Git offers effective tools for the same purpose. Modern Git users should prefer git restore for tracked files and git clean for untracked files, enabling confident and precise management of the working directory.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitVersion Controlgit restoregit checkoutgit cleandiscard changes
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.