Skip to content
LatchkeyLatchkey home

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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

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.

Frequently asked questions

git stash Command: Shelve Changes in CI?
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.
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.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card