How to Discard Unstaged Changes in Git

While working on a project you figure out that you made mistakes and need to undo everything. Here, you will get all answers related to the discarding of the unstaged changes in the working copy. All the changes that have been made but have not sent to the stage with 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:

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:

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:

git checkout -- <file>

Undoing changes with git reset

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

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 be called as unstaged. Staging the file will place the file 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 do another work, get back, and re-apply them. It takes both staged and unstaged changes, saves them for further use, and then returns them from your working copy. You can delete the stash with git stash drop. To remove all the stashes, you should use git stash clear.

How to stash only staged changes and discard the rest?

To stash only staged changes and discard the rest, you can use the --keep-index option with the git stash command. Here's the command:

git stash save --keep-index "message"

The --keep-index option tells Git to stash only changes that are staged (in the index), and leave the unstaged changes in the working directory. The save 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 staged changes only, and leave the unstaged changes in the working directory. You can then use git reset to discard the unstaged changes if needed:

git reset

This will reset the working directory to the last commit without affecting the staged changes that were stashed. 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 only the staged changes.

Cleaning Files

The git clean is an undo command that completes other commands like git reset and git checkout. Unlike the other commands, this command operates on files already added to the Git staging area and runs on untracked files. 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 -- with git checkout is a way to instruct Git what follows checkout to treat as a file, not a branch. 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 that you've made to your repository and reset it to a previous state. However, be careful when using git reset --hard because any uncommitted changes will be lost 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- Commit the changes made by running 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.