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

  1. Saves Changes: Temporarily saves the changes in our working directory and the index (staging area) to a new stash entry.

  2. Cleans Working Directory: Reverts the working directory and index to match the HEAD commit, making it clean.

Untracked and Ignored Files: By default, git stash does not include untracked or ignored files. Use the -u or -a options to include them.

Conflict Resolution: If applying a stash results in conflicts, you must resolve them manually and then continue working.

git stash apply

Stash vs. Commit: Use stashing for temporary work that you don't want to commit. For more permanent changes or milestones, it's better to commit the changes.

Stashing in CI/CD: Use git stash to temporarily save changes in continuous integration or deployment scripts when you need to perform operations that require a clean working directory.

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

  1. Removes Untracked Files: Deletes files that are not tracked by Git and are not ignored by .gitignore.

  2. 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