linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Everyday workflow/stash

kb/workflow ── Everyday workflow ── beginner

git stash

Saves uncommitted changes to a special stack and cleans the working tree back to HEAD. Useful when you need to switch branches urgently but are not ready to commit.

view as markdownaka: git-stash

A typical situation: you are editing files, not ready to commit, and you suddenly need to switch to another branch to review a PR or fix a hotfix. You do not want to commit a "WIP" and you do not want to lose your work.

git stash puts the working tree into a special "pocket". You can then switch and work elsewhere. When you come back, git stash pop restores the changes.

Basic cycle

bash
git stash                    # save the current changes
git switch hotfix            # switch to where you need to go
# ... do the work ...
git switch feature           # return to your branch
git stash pop                # restore the changes

By default stash takes both the working tree and the index (everything except untracked files). To include untracked files:

bash
git stash -u                 # + untracked
git stash -a                 # + untracked + ignored (everything)

With a message

Without a message the stash gets an auto-name like "WIP on feature: abc123 last commit subject". That is hard to read. Use a description:

bash
git stash push -m "fixed auth, logout not done yet"

Listing and inspecting

bash
git stash list
# stash@{0}: On feature: fixed auth, logout not done yet
# stash@{1}: WIP on main: 7c8a1 fix typo
git stash show stash@{0}            # summary
git stash show -p stash@{0}         # full diff

pop vs apply

  • git stash pop: apply and remove from the list.
  • git stash apply: apply, keep in the list.

apply is useful when you might need the stash again. pop is for when you know you are done with it.

Both commands take stash@{0} (the most recent) by default. You can specify another:

bash
git stash pop stash@{2}

stash branch

If the branch has moved far ahead since the stash was made and pop produces conflicts, you can recover the stash as a separate branch:

bash
git stash branch fix-from-stash stash@{0}

This creates a branch from the commit at which the stash was made and applies the stash in a clean environment. You can then work normally and merge.

What physically happens

A stash entry is a merge commit under refs/stash (invisible in normal branch listings). It has at least two parents:

  • first: HEAD at the time of the stash
  • second: a commit with a snapshot of the index

With -u or -a a third parent is added: a commit with a snapshot of untracked (and ignored) files. In total, one git stash creates 3-4 objects in .git/objects/. A stash can be recovered via reflog even after stash drop, by its stash@{N} SHA.

Pitfalls

  • Stash without -u does not hide untracked files. Untracked files are not lost; they stay in the working tree as-is. It just looks like "everything is stashed", yet after git switch or git checkout . those files are visible in the new context. To actually remove untracked files, use git stash -u.
  • A pop with a conflict does not delete the stash. If applying produced a conflict, the stash stays in the list. After resolving it, run git stash drop stash@{0} manually.
  • A long-lived stash is an anti-pattern. The longer a stash sits, the further main moves, the more painful pop becomes. If the work matters, create a branch and commit.
  • git stash clear deletes everything without confirmation. If you need to clean up, prefer git stash drop one entry at a time.

§ команды

bash
git stash

Save current changes (untracked files excluded)

bash
git stash -u -m "description"

With untracked files and a message

bash
git stash pop

Apply and remove the most recent stash

bash
git stash branch <name> stash@{0}

Recover a stash as a new branch (for conflict cases)

§ см. также

  • refloggit reflogA log of all HEAD movements on this machine. By default, entries for reachable commits live for 90 days; entries for unreachable commits live for 30. The primary recovery tool after `reset --hard`, `--amend`, `rebase`, and branch deletion. Stored locally in `.git/logs/`.
  • branchBranchA branch in Git is a file inside `.git/refs/heads/` that holds the SHA of one commit. Creating, switching, and deleting branches are trivial operations because a branch contains exactly 41 bytes of data.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies