Pular para o conteúdo
LatchkeyLatchkey home

O que é `git stash`?

git stash põe de lado temporariamente suas alterações não commitadas para que você possa trocar de contexto com um working directory limpo e restaurá-las depois.

git stash é a forma rápida de colocar seu trabalho atual na prateleira. Quando surge algo urgente e você ainda não está pronto para commitar, o stash guarda suas alterações não commitadas, deixa tudo limpo e permite retomar o trabalho quando você voltar.

O que o stash faz

O stash registra suas modificações não commitadas, tanto as staged quanto as unstaged, e reverte seu working directory para o último commit. As alterações são salvas em uma pilha de stashes que você pode reaplicar depois. É um botão de pausa leve, não um commit, e permanece inteiramente na sua máquina.

Guardar e restaurar

Você faz o stash, executa o trabalho urgente e depois faz pop do stash para trazer suas alterações de volta.

Shelving and restoring work
git stash push -m "WIP form layout"
# handle the interruption
git stash pop

Quando o stash ajuda

O stash é útil quando você precisa trocar de branch para corrigir algo urgente, trazer atualizações para uma árvore limpa ou remover temporariamente alterações para testar um build limpo. Você pode manter vários stashes e aplicá-los seletivamente, embora pilhas profundas de stash fiquem confusas.

Stash e CI/CD

Stashes são puramente locais e nunca são enviados por push, então o CI nunca os vê. Isso é intencional: um pipeline constrói o histórico commitado, não o trabalho em andamento que alguém guardou na prateleira. Se você quer que o CI teste algo, precisa commitar e fazer push; alterações no stash simplesmente não existem no remote.

Usando o stash com sabedoria

  • Use o stash para trocar de contexto sem commitar trabalho pela metade.
  • Adicione uma mensagem para lembrar o que cada stash contém.
  • Evite deixar os stashes se acumularem e ficarem obsoletos.
  • Lembre-se de que stashes são locais; commite para levar as alterações ao CI.

Applying this to your pipeline

  • Measure before changing. Most CI optimisation targets the wrong step because the slow one is assumed rather than timed.
  • Cache what is expensive to produce and cheap to validate, and key the cache to the exact tool version.
  • Fail fast: run the cheapest checks that can reject a change first, so an expensive job never starts on code that cannot pass.
  • Prefer determinism over speed when they conflict. A fast pipeline nobody trusts gets re-run, which is slower than a slow one that is believed.

Principais conclusões

  • git stash guarda alterações não commitadas para deixar um working directory limpo.
  • Você as restaura depois com pop ou apply a partir da pilha de stashes.
  • Stashes são apenas locais; o CI constrói o histórico commitado, não os stashes.

Perguntas frequentes

What is What is git stash??
git stash is the quick way to put your current work on a shelf. When something urgent comes up and you are not ready to commit, stashing tucks your uncommitted changes away, gives you a clean slate, and lets you bring the work back when you return.
What stashing does?
Stashing records your uncommitted modifications, both staged and unstaged, and reverts your working directory to the last commit. The changes are saved on a stash stack you can reapply later. It is a lightweight pause button, not a commit, and stays entirely on your machine.
Stashing and restoring?
You stash, do the urgent work, then pop the stash to bring your changes back.
When stashing helps?
Stashing is handy when you need to switch branches to fix something urgent, pull in updates onto a clean tree, or temporarily clear changes to test a clean build. You can keep multiple stashes and apply them selectively, though deep stash stacks get confusing.

Guias relacionados