Stashing and Cleaning
git stash
Description
It is used to temporarily save changes in our working directory and staging area that we don't want to commit yet. This allows to switch branches, pull updates, or perform other tasks without losing our current work. We can later apply the stashed changes back to the working directory.
Usage
git stash [push] [<options>] [<message>]
Options
-u
or --include-untracked
: Stash untracked files in addition to tracked files.
-a
or --all
: Stash all files, including untracked and ignored files.
-m <message>
: Include a message with the stash for easier identification.
--keep-index
: Keep the index intact (only stash unstaged changes).
--patch
: Interactively select hunks to stash
git stash push -u
git stash push -a
git stash push -m "Work in progress"
git stash push --keep-index
git stash push --patch
What It Does
Saves Changes: Temporarily saves the changes in our working directory and the index (staging area) to a new stash entry.
Cleans Working Directory: Reverts the working directory and index to match the HEAD commit, making it clean.
Common Use Cases
-- Basic Stash
-- Saves all local modifications (both staged and unstaged) and reverts the working directory to the last committed state
git stash
-- Stash with a Message
-- Saves the changes with a message for easier identification
git stash push -m "WIP: Adding new feature"
-- Stash Only Unstaged Changes
-- Stashes only the changes that have not been staged (keeps the index intact).
git stash push --keep-index
-- List Stashes
-- Lists all stashes in the repository
git stash list
-- Apply the Most Recent Stash
-- Applies the most recent stash to the working directory
git stash apply
-- Apply a Specific Stash
-- Applies the specified stash (by index) to the working directory
git stash apply stash@{1}
-- Pop the Most Recent Stash
-- Applies the most recent stash and then removes it from the stash list
git stash pop
-- Drop a Specific Stash
-- Deletes a specific stash from the list
git stash drop stash@{1}
-- Clear All Stashes
-- Removes all stashes
git stash clear
Example Output
When running git stash push -m "WIP: Adding login feature"

When running git stash list

git clean
Description
It is used to remove untracked files and directories from the working directory. This is particularly useful for cleaning up the workspace by getting rid of files that are not under version control and are not needed, such as build artifacts, temporary files, or files generated by tools.
Usage
git clean [<options>]
Options
-n
or --dry-run
: Show what would be done without actually doing it.
-f
or --force
: Required to actually delete files.
-d
: Remove untracked directories in addition to untracked files.
-i
or --interactive
: Show what would be done and ask the user whether to proceed.
-q
or --quiet
: Suppress all output.
-e <pattern>
or --exclude=<pattern>
: Exclude files matching the pattern from being removed.
-x
: Remove all untracked files, including those ignored by .gitignore
.
-X
: Remove only files ignored by .gitignore
.
git clean -n
git clean -f
git clean -fd
git clean -i
git clean -f -q
git clean -f -e "*.txt"
git clean -fx
git clean -fX
What It Does
Removes Untracked Files: Deletes files that are not tracked by Git and are not ignored by
.gitignore
.Removes Untracked Directories: Optionally deletes directories that are not tracked by Git.
Common Use Cases
-- Remove Untracked Files from the working directory
git clean -f
-- Remove both untracked files and untracked directories from the working directory
git clean -fd
-- Force Removal
git clean -f
-- Interactive Mode
-- Allows to interactively choose which files to remove.
git clean -i
-- Remove Ignored Files
-- Removes only files ignored by .gitignore
git clean -X
-- Remove All Untracked and Ignored Files
-- Removes all untracked files, including those ignored by .gitignore
git clean -x
Example Output
When running git clean -n

When running git clean -i

Last updated
Was this helpful?