How do you stash an untracked file?

 

To stash an untracked file in Git, you can use the git stash command with the -u or --include-untracked option. Here's an example:

  1. Create an Untracked File:

    Start by creating an untracked file in your Git repository. For example, let's create a new file named newfile.txt:

    bash
  • echo "This is an untracked file." > newfile.txt
  • Stash the Untracked File:

    Use the git stash command with the -u or --include-untracked option to stash the untracked file:

    bash
  • git stash -u

    The -u option tells Git to include untracked files in the stash. After running this command, the untracked file newfile.txt will be stashed.

  • Check the Stash:

    You can check the contents of your stash using the git stash list command:

    bash
  • git stash list

    This will display a list of stashed changes, including the untracked file.

  • Apply or Pop the Stash:

    To retrieve the stashed untracked file, you can use the git stash apply or git stash pop command followed by the stash reference (e.g., stash@{0} for the most recent stash):

    bash
    1. git stash apply stash@{0}

      This will apply the stash to your working directory, and you'll find the previously untracked file newfile.txt in your working directory.

    Remember to replace stash@{0} with the appropriate stash reference if you have multiple stashes.

    Stashing untracked files is useful when you want to temporarily save changes, including untracked files, without committing them, allowing you to work on something else or switch branches.

    Comments