Skip to content
Latchkey

git stash Command: Shelve Changes in CI

git stash records uncommitted changes and reverts the working tree to a clean state.

Stash is mostly an interactive tool, but CI sometimes uses it to set local changes aside before a fetch or rebase, then restore them. Used carefully it keeps a script idempotent.

Common flags

  • push - stash current changes (the default action)
  • -u / --include-untracked - also stash untracked files
  • -m <msg> - give the stash a descriptive message
  • pop - apply the most recent stash and drop it
  • apply - apply a stash but keep it in the list
  • list - show saved stashes

Example

shell
# Set local changes aside, update, then restore them
git stash push -u -m "ci-temp"
git pull --rebase origin main
git stash pop

In CI

Be cautious: git stash pop can conflict and leave the tree in a partial state, failing the job mid-script. On ephemeral runners a clean checkout is usually simpler than stashing. Reserve stash for the rare case where you must preserve generated state across a rebase.

Key takeaways

  • git stash -u shelves untracked files too, which the default does not.
  • A pop can conflict, so handle its failure explicitly in scripts.
  • On disposable CI runners, a fresh checkout often beats stashing.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →