Git

How Can I Discard Modified Files?

While working on Git, developers make modifications to their project files. They stage those modifications and save them. However, sometimes, they do not want to keep the new changes and want to discard those modifications. In this situation, Git allows them to discard modified files wherever they want.

This article will demonstrate:

How to Discard Tracked and Untracked Modified Files?

To discard all the tracked and untracked modified files, the “git reset –hard” command is used. Follow the provided steps, to do so.

Step 1: View Repository Status

First, utilize the “git status” command and view the repository’s current status:

git status

The above output indicates that the repository contains some tracked and untracked modified files.

Step 2: Discard All Modified Files

To discard all the tracked and untracked modified files, run the below-listed command:

git reset --hard

The output shows that all modified files have been discarded.

Step 3: Verify Changes

Lastly, view the Git status again to ensure changes:

git status

The above output shows that Git status is clear now which means that the modified files’ modifications have been discarded.

How to Discard Only Untracked Modified Files?

To discard untracked modified files, utilize the “git checkout .” command. For a better understanding, check out the following steps:

Step 1: Check Git Status

First, view the repository’s current status:

git status

The above output shows two untracked modified files in the repository.

Step 2: Discard Untracked Modified Files

Then, utilize the following command to discard untracked modified files:

git checkout .

The output confirms that untracked modified files have been discarded.

Step 3: Verification

To verify whether the modified files have been discarded or not, view the repository status:

git status

The above output indicates that the untracked modified files’ changes have been discarded:

That was all about discarding modified files in Git.

Conclusion

To discard all the tracked and untracked modified files, the “git reset –hard” command is used. To discard only untracked modified files, utilize the “git checkout .” command. To verify whether the modified files have been discarded or not, view the repository status via the “git status” command.

This article has demonstrated the methods to discard modified files in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.