# 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

```sh
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.

{% hint style="info" %}
**Untracked and Ignored Files**: By default, `git stash` does not include untracked or ignored files. Use the `-u` or `-a` options to include them.

<pre><code><strong>git stash push -u
</strong></code></pre>

**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.
{% endhint %}

### 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"`

<figure><img src="/files/JRCj0yzTcM3GeIfqdjBO" alt="" width="563"><figcaption></figcaption></figure>

When running `git stash list`

<figure><img src="/files/NwxU2YnYZwPJqBLqRc9e" alt="" width="431"><figcaption></figcaption></figure>

## 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

```sh
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

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

```
-- 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`

<figure><img src="/files/rE2zwmg3onel2snMI9Nz" alt="" width="270"><figcaption></figcaption></figure>

When running `git clean -i`

<figure><img src="/files/EA1LFUeQnHsOmwwIuqmB" alt="" width="344"><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/git/commands/stashing-and-cleaning.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
