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
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
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
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
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
.
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
Example Output
When running git clean -n
When running git clean -i
Last updated
Was this helpful?