Which option is used to stash your untracked files?

Understanding the Git Stash -u Option

Git is an essential tool for software developers and helps them maintain and organize their codebase effectively. Among its various functionalities is the powerful git stash command, often used when developers want to switch contexts without committing their ongoing changes.

The git stash command can save your changes on a new stack, allowing you to easily revert to the previous state of your project. But the default behavior of git stash will only secure modified and staged tracking files. So, what to do when you have untracked or ignored files that you also want to stash?

That's when the git stash -u option comes into play. The -u option, or --include-untracked, is used to stash your untracked files. This is very useful in case you've added new files to your project that haven't been tracked yet and you want to change your branch or revert to a previous state.

Let's take a practical example. Suppose you've added a new feature.js file in your project. It's still in progress and has not yet been tracked. However, you need to suddenly switch to solve a bug in another branch. You don't want to lose your work but don't want to commit it either as it's incomplete. In this case, you can run:

git stash -u

This will stash your changes as well as the untracked feature.js file. You can now easily switch branches and when you're ready to back to your work, you can apply the stash and get all changes, including the untracked files.

It's good to note that while -u or --include-untracked stashes untracked files, it does not stash ignored files. If you want to stash ignored and untracked files, consider using -a or --all instead.

To conclude, using git stash -u is a best practice when working with new files in your Git repository that you want to stash without committing. It gives you the flexibility to switch branches or tasks, without losing the progress on your untracked files.

Do you find this helpful?