To discard changes in Git, you can use the following commands depending on whether you want to discard changes in tracked files or untracked files.
Discard Changes in Tracked Files
If you want to discard changes in a tracked file (i.e., changes that have been modified but not yet committed), use:
git checkout -- <file>
For example, to discard changes in a file named example.txt, run:
git checkout -- example.txt
Discard All Untracked Changes
If you want to discard all untracked changes (files that are not being tracked by Git), use:
git clean -f -d
This command will remove all untracked files and directories.
Summary
- Use
git checkout -- <file>to discard changes in tracked files. - Use
git clean -f -dto discard all untracked changes.
Make sure to use these commands carefully, as they will permanently remove your changes.
