You're building the shopping cart feature, and the code is half done. Then you hear the news: a typo on the home page has to be fixed right now. Commit code that doesn't work yet? Not nice. Throw it away? What a waste.
git stash is the way out. It puts changes you haven't committed into a temporary place, so your working folder is clean again. It's like cooking when guests arrive and need the kitchen table. You put the half-chopped ingredients into a container, and the container into the fridge. Once the guests leave, you take the container out and continue cooking from there. In Git, the kitchen table is your working folder, and the half-chopped ingredients are the changes you haven't committed. The container in the fridge is the stash, and taking the container out is git stash pop.
This lesson also covers .gitignore: a file that lists the files Git shouldn't track at all. A tracked file is one that Git records, so its changes appear in git status.
Stash: save it for later, handle the emergency
The cake-shop repo has two branches. On main, index.html still has a typo: "Cake Shopp". On feature-cart, the last commit is 5e2b7a1 add cart. You're changing app.js on feature-cart, and the change is not finished and not committed. Suddenly the typo on main has to be fixed right now. The example below tries to switch to main, then runs git stash, then tries to switch again:
$ git switch main
error: Your local changes to the following files would be overwritten by checkout:
app.js
Please commit your changes or stash them before you switch branches.
Aborting
$ git stash
Saved working directory and index state WIP on feature-cart: 5e2b7a1 add cart
$ git switch main
Switched to branch 'main'
- Git refuses to switch because
app.jsonmaindiffers fromapp.jsonfeature-cart. Switching would overwrite your uncommitted changes. (If the files you changed are the same on both branches, Git switches and keeps your changes in the working folder.) git stashsaves the changes in the working directory (your working folder) and in the staging area, then makes the files match the last commit again. Your working folder is clean, sogit switchnow works.WIP on feature-cart: 5e2b7a1 add cartmeans half-done work (WIP, work in progress) from thefeature-cartbranch, on top of commit5e2b7a1.
On main, you fix the typo and make the commit fix typo. Then you go back to feature-cart and get your work back:
$ git switch feature-cart
Switched to branch 'feature-cart'
$ git stash pop
On branch feature-cart
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: app.js
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8c41d2e0b6f9a3c7d5e1f2a4b8c9d0e6f7a3b1c5)
After pop, Git prints the output of git status: app.js has your changes again, under Changes not staged for commit. The last line says the stash was removed from the stash list. The long hash at the end will be different on your laptop.
The stash list: list, pop, apply, drop
You can have more than one stash, kept in a pile. The newest is always stash@{0}. The one before it moves down to stash@{1}, and so on. git stash list shows the list:
$ git stash list
stash@{0}: WIP on feature-cart: 5e2b7a1 add cart
| Command | What happens |
|---|---|
git stash |
save the changes, the working folder is clean again |
git stash list |
show every stash, the newest is stash@{0} |
git stash pop |
bring back stash@{0}, then remove it from the list |
git stash apply |
bring back stash@{0}, but keep it in the list |
git stash drop |
throw stash@{0} away without bringing it back |
git stash push -m "message" |
stash with a name of your own, so it's easy to find |
You'll usually use pop. apply is useful when you want to put the same changes on two branches. The push in git stash push has nothing to do with git push: the stash stays on your laptop.
.gitignore: the list of files not to track
Some files must never go into a repo:
node_modules/: thousands of files fromnpm install(a command that downloads JavaScript libraries for your project). They can be made again at any time..env: a file with passwords and API keys (secret keys for using other services).- log files: notes that a program writes while it runs.
- build output: the finished files made from your code.
Write their patterns in a .gitignore file, in the repo's top folder. A pattern is a rule that matches file names. This is an example .gitignore:
# from npm install, can be rebuilt
node_modules/
# secrets: passwords, API keys
.env
# every file ending in .log
*.log
# except this one
!important.log
node_modules/: a slash at the end means a folder, with everything inside it..env: one exact file name.*.log:*matches any name, so this pattern matches every file ending in.log.!important.log: a!cancels the rule above it, so this file still appears.- A line that starts with
#is a comment.
Commit the .gitignore file too, so its rules apply to the whole team. This table shows what it does to git status, in a repo where node_modules/ was already committed:
| File | Without .gitignore |
With the .gitignore above |
|---|---|---|
.env |
listed under Untracked files | hidden |
debug.log |
listed under Untracked files | hidden |
important.log |
listed under Untracked files | still listed, because of ! |
node_modules/ (already committed) |
tracked | still tracked |
Files that were already committed
.gitignore only applies to files that are not tracked yet (untracked). So a node_modules/ that was already committed is still tracked, and its changes still appear in git status. Remove the folder from tracking first:
git rm -r --cached node_modules/ # stop tracking, the files on your laptop stay
git commit -m "stop tracking node_modules"
git rm --cachedremoves files from Git's tracking (from the staging area), but does not touch the files on your laptop.-r(recursive: with everything inside it) is needed becausenode_modules/is a folder.- After this commit,
node_modules/is untracked. Since it matches.gitignore, it no longer appears ingit status. - Without
--cached,git rmdeletes the files from your laptop as well.
⚠️ Stashes left behind, secrets that leaked
- Stash doesn't take new files. Files that were never
git added (untracked) are left in the working folder. Usegit stash -u(here-uis short for--include-untracked) to save them too. - A
popthat clashes. If the stashed changes clash with the branch you run pop on, you get a conflict, and the stash is not removed. Fix the conflict, then rungit stash drop. - Forgotten stashes. The stash list can grow without you noticing. Check it with
git stash list, and name your stashes withgit stash push -m "half-done cart". - A
.envthat was already pushed..gitignoreandgit rm --cacheddon't remove it from old commits. Its contents can still be read in the history, so change the password or API key.
TL;DR: git stash saves uncommitted changes in a temporary place (a stash) and cleans the working folder. git stash list shows the list of stashes (the newest is stash@{0}). git stash pop brings a stash back and removes it from the list, git stash apply brings it back but keeps it, and git stash drop throws it away. .gitignore is a file of patterns (rules for file names) for files Git should not track (record): node_modules/ for a folder, *.log for every .log file, and ! for an exception. .gitignore doesn't apply to files that are already committed: remove them from tracking first with git rm -r --cached node_modules/, then commit.
⚡ Save it for later, then switch
Write:
git stash
git switch main
Meaning: git stash puts your uncommitted changes in a temporary place, and your working folder is clean again. Without a stash, Git refuses to switch branches if those changes would be overwritten. After the stash, git switch works.
⚡ Get your work back
Write:
git switch feature-cart
git stash pop
Meaning: First go back to the branch you were working on, here feature-cart. Then pop brings back the newest stash, stash@{0}, and removes it from the stash list. git stash list shows every stash still saved.
⚡ Files not to track
Write:
node_modules/
.env
*.log
Meaning: Write these patterns (rules for file names) in a .gitignore file, so that Git doesn't track (record) the files that match. The slash at the end of node_modules/ means a folder, .env is one file name, and *.log matches every file ending in .log. Commit the .gitignore too, so the rules apply to the whole team.
⚡ Already committed
Guess first: node_modules/ was already committed, and only then written into .gitignore. Is that enough to make Git stop tracking it?
Not yet. .gitignore only applies to files that aren't tracked. Remove it from tracking first with git rm -r --cached node_modules/, then commit.
Meaning: --cached only removes files from Git's tracking. The files on your laptop stay. -r is needed because node_modules/ is a folder.