How to Discard all Changes in Git?
Last Updated :
24 May, 2024
Git is a popular version control tool that remembers all the changes made to a set of files over time. This is especially useful for software development, where multiple people might be working on the same code.
What are the Changes in Git?
1. Changes you make to your files
As you edit files in your project, Git keeps track of those modifications. These changes can be staged (added to a queue for the next commit) or unstaged (not yet added to the queue).
2. Changes reflected in the commit history
Whenever you commit your staged changes, Git creates a snapshot of your project. The commit history allows you to see how your project has evolved, including who made what changes and when.
Here are some commands to help you see different types of changes:
This command shows the unstaged changes in your working directory:
git diff
This shows the changes you've staged for the next commit:
git diff --staged
This shows the history of commits in your project:
git log
State of git changes
Staged Changes
Staged changes are those that have been marked to be included in the next commit. You stage changes using the git add command. These changes are in the staging area (also known as the index). When you commit, Git takes the snapshots of these staged changes and records them in the repository's history.
Unstaged Changes
Unstaged changes are modifications made to files in your working directory that Git is not currently tracking or has not been staged for commit. These changes can be viewed with the git status command. Unstaged changes could be new files that Git is not aware of or modifications to existing tracked files that have not been staged with git add.
staging in gitHere we can see that unstaged changes can be viewed from U mark next to file name and A mark for staged changes using git add command.
Here's a basic workflow:
- Make changes to your files in the working directory.
- Use "git add <file_name>" to stage changes you want to include in the next commit.
- Use "git commit" to commit the staged changes to the repository.
- Use "git status" to see the status of your files, including staged and unstaged changes.
Example: To demonstrate the basic workflow of working with git.
OutputTo discard all changes in Git, including uncommitted changes, staged changes, and untracked files, you can follow these steps which are as follows:
1. Discard Uncommitted Changes in Tracked Files
To discard changes in tracked files (both staged and unstaged), use the following command:
git reset --hard
This will reset the working directory and the staging area to match the most recent commit. Note that this will discard all local changes that have not been committed.
Example: To demonstrate discarding uncommited changes in the tracked files in git.
git resetNote: Here after using git reset --hard the deleted files(example.py,dummy2.py) is back as these changes were uncommitted.
2. Remove Untracked Files and Directories
To remove untracked files and directories, use the following commands:
Untracked Files:
git clean -f
Untracked Directories:
git clean -fd
To see what will be removed before actually deleting the files, use:
git clean -n
Example: To demonstrate removing untracked files and directories in git.
git clean usageNote: Here we can see that using git clean -n is informing user what would it remove then following git clean -f actually removed the untracked file dummy.py.
3. Discard staged changes in Git
git reset HEAD
This command will unstage all changes that have been added to the staging area, effectively undoing the git add operation. However, it won't modify the actual files in your working directory; it only resets the staging area to the state of the last commit.
Example: To demonstrate discarding staged changes in git.

Note: Here we can observe that after staging ex.py using git add command using git reset HEAD ex.py the file goes back to unstaged state and is shown using U next to file name.
Make sure to use these commands with caution, as they will discard any changes you've made since the last commit.
5. Can I recover discarded staged changes?
If you haven't committed the changes yet, discarding staged changes effectively removes them from the staging area. However, if you've committed the changes, you can still recover them using Git's history and version control features.
Similar Reads
How to Discard Changes in Git?
In Git, "discard changes" refers to reverting modifications made to files in your working directory back to their state in the last commit, effectively disregarding any changes you've made since then. This action can be useful if you've made changes that you no longer want to keep or if you want to
2 min read
How to see the Changes in a Git commit?
Understanding the changes introduced in each commit is crucial for effective collaboration and version control in Git. Whether you are reviewing someone else's work or tracking your own modifications, Git provides powerful tools to inspect changes. This article will guide you through the various met
4 min read
How To Pull All Branches In Git?
In Git, pulling all branches is the process of fetching the latest changes from a remote repository and merging them into the local branches. This makes sure that the local copy of the repository is up-to-date with the remote version, allowing us to access and work with the most recent code across a
2 min read
How to Fetch All Git Branches?
In Git, we create multiple branches simultaneously to develop features, fix bugs, or experiment with new ideas. Understanding how to fetch and manage Git branches is important for maintaining a clean and up-to-date repository. In this article, we'll explore how to fetch all Git branches. Table of Co
2 min read
How to Check Branch in Git?
In Git, branches are independent lines of development that allow you to work on features or fixes without affecting the main codebase. Checking branches is important for managing project history, coordinating collaboration, and ensuring smooth development workflows. This article will walk you throug
2 min read
How to Change Branch Name in Git?
Git is a powerful version control system widely used for managing code changes in software projects. One common task that developers encounter is renaming branches. Whether due to a change in project scope, a shift in naming conventions, or a simple typo, knowing how to rename branches efficiently i
3 min read
How to Delete All Local Branches in Git?
Git provides a powerful version control system that allows developers to collaborate efficiently on projects. Over time, however, a repository can have a lot of local branches that are no longer needed, cluttering the workspace and potentially causing confusion. The "main" is the by default branch c
3 min read
How to Dry Run Git Commands?
Git is used for version control in software development. Understanding the effects of Git commands before executing them can help prevent mistakes and ensure that your repository remains in a good state. This is where "dry running" Git commands become incredibly useful. A dry run simulates the execu
3 min read
How To Compare Branches on GitHub?
One important aspect of branch management is comparing branches to review differences in code, commits, or pull requests. Whether youâre working on new features, resolving conflicts, or reviewing code, comparing branches is a common task in GitHub.In this article, we will cover different ways to com
5 min read
How to Add All Files in Git ?
Adding all files in Git is a common task when you want to stage all changes for committing. Adding all files in Git involves staging all modifications, additions, and deletions in your working directory for the next commit. This process ensures that all changes are included in the commit history. In
3 min read