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
shell
# 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.
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
git reset Command: Move HEAD in CIgit reset moves HEAD and optionally the index and working tree. Reference for --hard and --soft to roll back…
git clean Command: Remove Untracked Files in CIgit clean deletes untracked files. Reference for -f, -d, and -x (git clean -fdx) to fully reset a working tre…
git status Command: Check Tree State in CIgit status shows working tree and index state. Reference for --porcelain, the stable machine-readable format…
git checkout Command: Switch Refs in CIgit checkout switches branches or restores files. Reference for checking out a branch, --detach, and -b to cr…