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 messagepop- apply the most recent stash and drop itapply- apply a stash but keep it in the listlist- show saved stashes
Example
# Set local changes aside, update, then restore them
git stash push -u -m "ci-temp"
git pull --rebase origin main
git stash popIn 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.
- 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 detachedKey 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.