W3docs

How to Discard Unstaged Changes in Git

The tutorial provides you with information you need to discard the unstaged changes in the working copy. Find several ways of discarding and get the codes.

While working on a project, you may realize you made mistakes and need to undo everything. This guide covers how to discard unstaged changes in the working copy. All changes that have been made but not yet added to the staging area with the git add command are considered unstaged changes.

There are three options in Git that help to undo your local changes

To view the changes that have been made in your working directory, you should run git status:

use git status

git status

Undoing changes with git stash

To discard all local changes, but also to save them for later use, you can run the git stash command:

use the git stash drop

git stash

For more information, refer to How to Stash Git Changes.

Undoing changes with git checkout

To discard local changes to a file permanently, you can run:

discard local changes to a file

git checkout -- <file>

Undoing changes with git reset

To discard all local changes to all the files permanently, you can do:

discard all local changes to all files

git reset --hard

Staged and Unstaged Changes

The staging area (index) is a container where Git gathers all changes which will be part of the next commit.

Editing a versioned file on the local machine will be recognized by Git as a modified file and considered unstaged. Staging the file places it into the staging area. The changes within the staging area are part of the next commit. The next commit will transfer all items from the staging area into your repository. The staging area allows collecting all changes to get a clean commit.

Stashing Work

The git stash command shelves changes made to your working copy so you can work on something else, get back, and re-apply them. It takes both staged and unstaged changes, saves them for further use, and then removes them from your working copy. You can delete a specific stash with <kbd class="highlighted">git stash drop</kbd>. To remove all stashes, you should use <kbd class="highlighted">git stash clear</kbd>.

How to stash only staged changes and discard the rest?

To stash unstaged changes while keeping staged changes in the index, you can use the --keep-index option with the git stash command. Here's the command:

How to stash only staged changes and discard the rest?

git stash push --keep-index -m "message"

The --keep-index option tells Git to stash unstaged changes while keeping staged changes in the index. The -m option is used to give a message to the stash for future reference.

After running the above command, Git will create a new stash with the unstaged changes, while keeping the staged changes in the index. If you want to unstage the remaining changes, you can use git reset HEAD:


git reset HEAD

This will unstage the changes. To apply the stashed changes later, you can use the git stash apply command:


git stash apply

This will apply the latest stash, which contains the unstaged changes.

Cleaning Files

The git clean command removes untracked files and directories from the working directory. Unlike the other commands, this command only operates on files that are not yet tracked by Git. Untracked files are those created within the working directory but are not yet added to the staging area.

Git Checkout vs Git Checkout --

The git checkout command switches branches or restores working tree files. It operates on files, commits, and branches and allows switching between multiple features in just a single repository. The command works with the git branch command. It updates the files in the working directory to match the version stored in that branch, instructing Git to record all the new commits.

The -- separator with git checkout instructs Git to treat what follows as a file path rather than a branch name. It is used when, for example, you have a file and a branch with the same name.

This command is useful when you want to discard changes made to a specific file and reset it to its last committed state. Be careful when using git reset --hard instead, as it discards all uncommitted changes and cannot be recovered.

How to undo changes to a single edited file while retaining changes to all other edited files?

There is a way to undo changes to a single edited file while retaining changes to all other edited files using the git checkout command. Here's how you can do it:

1- First, run git status to see which files have been modified.

2- Identify the file that you want to undo changes for, and copy its path.

3- Run the following command, replacing <path-to-file> with the actual path of the file:


git checkout -- <path-to-file>

This command will discard any changes made to the specified file since the last commit, effectively reverting it back to its last committed state.

4- If you want to commit the remaining changes, run the following commands:


git add .
git commit -m "Revert changes to <file-name>"

This will stage and commit the changes made to all other edited files, while leaving the reverted file unchanged.

Note that you should use this command with caution, as it will permanently discard any changes made to the specified file since the last commit. It's always a good idea to create a backup or clone of your repository before making any significant changes.