Git contains a Staging Area(or Index) to sort and segregate files. The Git Add command adds files to the staging area, and these files and changes will be part of the next commit. Files in the staging area are known as staged files(staged changes). Any change that has not been added to the staging area is called an unstaged change.
When we modify an existing file or create a new one, then these files(or the modifications) are unstaged. Let's learn how to discard the unstaged changes and untracked files in Git.
An Untracked File is a file that is created in our repository but never added to the staging area. We can use the Git Clean command to discard untracked files. This command will delete any untracked files present in our repo.
git clean -df
The -d option will remove untracked directories. The -f(short for --force) option is used to clean the untracked files even when the Git configuration variable clean.requireForce is not set to false.
The Git Checkout command has multiple uses. One of them is to undo changes. We can use the Git Checkout command to discard unstaged changes.
Pass the file path to the Git Checkout command to discard unstaged changes from a single file. We use -- to remove argument ambiguity.
git checkout -- <file-path>
To discard all unstaged changes, pass a period(.) to the command.
git checkout -- .
Git Restore is a relatively new command and was introduced in version 2.23. As the name suggests, the Git Restore command restores changes. We can use this command to discard unstaged changes. We can pass the file path to discard changes of a single file. Or we can use the period(.) to discard all the unstaged changes.
git restore <file-path> # for a single file
git restore . # to discard all unstaged changes
The Git Stash command is used to stash or safely store changes. The Git Stash command is perfect if we want to discard the unstaged changes from a file but also store them for any future use.
We will use the --keep-index option with the Git Stash Save command. The --keep-index option keeps our staged files intact. We can also use the --include-untracked option to stash and discard untracked files. The --include-untracked option internally uses the Git Clean command to discard untracked files.
git stash save --keep-index --include-untracked
We can drop the stashed changes by using the Git Stash Drop command.
git stash drop
Consider a repository with two files - demo.txt and demo2.txt. The demo.txt file is a modified file and the demo2.txt file is untracked. Both of them are unstaged. We can verify this by running the Git Status command.
git status
Output:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: demo.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
demo2.txt
no changes added to commit (use "git add" and/or "git commit -a")
Let's first discard the changes of the demo.txt file. We can use any one of the following commands
git checkout -- demo.txt
git restore demo.txt
git stash save --keep-index # will stash the changes
Let's verify whether the changes of demo.txt were discarded.
git status
Output:
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
demo2.txt
nothing added to commit but untracked files present (use "git add" to track)
Let's discard demo2.txt. We can either use Git Clean command, or we can use the Git Stash Save command with --include-untracked option.
git clean -df
git stash save --keep-index --include-untracked # will stash the changes
Let's verify with the help of Git Status command.
git status
Output:
On branch master
nothing to commit, working tree clean
Unstaged changes are any changes that have not been added to the Staging Area(or the Index). These can include both modified and untracked files.
The Git Clean command is our best option for discarding untracked files. This command simply deletes all the untracked files.
To discard modified unstaged files, we can use the Git Checkout, the Git Restore, or the Git Stash command. Git Restore is a new addition and will not work on older Git versions.
The Git Stash command will store the changes before discarding them. We can use these saved changes at any time in the future. Or we can drop them by using the Git Stash Drop command.