Removing Uncommitted Files in Git
5 min read·Jan 1, 2025
In Git, uncommitted files are all the files present in the working tree or staging area whose changes have not yet been committed to the repository.
Periodically removing files from these areas allows you to keep your workspace clean and organized, and also to:
- Avoid committing development files, such as test data, personal notes, temporary scripts, etc.
- Avoid committing sensitive data or configuration files that should not be part of the repository.
- Prevent unintended side effects during the build process or the execution of test suites.
- Manage disk space more efficiently, especially in environments with limited storage.
Removing files from the working tree
Removing untracked files
To remove all the files and directories from the working tree that are not currently tracked by Git, such as temporary files or build artifacts, you can use the git clean command:
$ git clean
This command will recursively remove all the files starting from the current directory.
Removing tracked files
To remove all the files and directories from the working tree that are tracked by Git, you can use the git rm command:
$ git rm file ...
This command will physically remove the specified files from the working tree and add them to the staging area for deletion.
Note: Under the hood, the
git rmcommand combines the Unixrmandgit addcommands in a single step.
Example
In this example, the git add command will remove the script.js file from the working tree and mark it as "deleted" in the staging area:
$ git rm script.js
$ git status
On branch main
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: script.js
Removing files from the staging area
It may happen that some of the files present in the staging area require additional work before being committed to the repository, or were accidentally added and should not be part of the next commit.
To remove files from the staging area while preserving the changes made to the working directory, you can use the git reset command:
$ git reset <file ...>
Alternatively, to unstage all the files at once, you can use the git reset command without arguments:
$ git reset
Example
In this example, the git reset command will remove the index.js file from the staging area, resulting in Git seeing the file as modified to but staged:
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.js
$ git reset index.js
Unstaged changes after reset:
M index.js
$ git status
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: index.js
no changes added to commit (use "git add" and/or "git commit -a")
Summary
Here's a summary of what you've learned in this lesson:
- The
git cleancommand is used to remove untracked files from the working tree. - The
git rmcommand is used to remove tracked files from the working tree and stage them for commit. - The
git resetcommand is used to remove files from the staging area.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery