stash -u
- Git course
- Advanced
- Lesson 33/56
- 4 min
Learn to stash untracked files with stash -u.
Include untracked files in a stash
The normal git stash command does not save untracked files. If you also want to temporarily remove newly created files, you can use the -u option. It includes both changes to tracked files and untracked files in the stash.
git status
git stash -u
git statusAfter the command, untracked files are also removed from the working directory and saved in the stash entry.
Restore untracked files
Untracked files saved with git stash -u return to the working directory when the stash entry is reapplied. For example, git stash pop applies the saved changes and removes the corresponding stash entry from the list.
git stash -u
git stash list
git stash popThis approach helps protect newly created files that you have not committed yet when switching to another task.
Practice Stash untracked files
Create a commands array. Add git status as the first element and git stash -u, which also saves untracked files, as the second element. Print the array using console.log.
Starter code
const commands = [
// Komutları sırayla ekle
];
// Komutları yazdırSample solution
let commands = [
"git status",
"git stash -u"
];
console.log(commands.join("\n"));Expected output
git status git stash -u
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which option includes newly created files that are not yet tracked by Git in a stash?
- -u Correct answer
- --soft
- --tracked
- --patch
Explanation: <code>-u</code> or <code>--include-untracked</code> also includes untracked files in the stash.
2 Include untracked files
git stash ____
git stash -u
Explanation: The <code>-u</code> option includes untracked files in the stash entry.
3 Stash file states
| Term | Meaning |
|---|---|
Tracked |
Tracked by Git |
Untracked |
Not tracked yet |
git stash |
Saves tracked changes |
git stash -u |
Includes new files too |