Open In App

How to Discard all Changes in Git?

Last Updated : 24 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Screenshot-2024-05-24-101244
staging in git

Here 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.

Screenshot-2024-05-24-101624
Output

To 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.

Screenshot-2024-05-24-101758
git reset

Note: 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.

Screenshot-2024-05-24-101846
git clean usage

Note: 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.

Screenshot-2024-05-24-103511

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.


Next Article
Article Tags :

Similar Reads