Git Stash and Git Tags
Level 3 · Undo and set aside
How git stash parks half-done work so you can switch branches. How a tag names a release like v1.0, and why tags need a push of their own.
Course contentsLesson 8 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Park changes with git stash
- Mark a release with a tag
- Push a tag so others can see it
11 mincovers 3 glossary entries
Worth reading first: Undo a Git Commit
How to park work, and how to name a release
Git stash takes the changes you have not committed and puts them aside, leaving your files clean. Git stash pop brings them back. It is for the moment you must switch branches but are not ready to commit. Stashes pile up with the newest on top, and the top one is called stash@{0}.
Say you are halfway through a new form when a bug report comes in. Git may refuse to switch branches, because your changes would clash with the other branch's files. You stash, switch, fix the bug, switch back and pop. Your half-done form is back exactly as you left it.
Git stash list shows every stash, with the branch it came from and the commit it sat on. Git stash pop takes the top one back and removes it from the pile. If the pop clashes with changes made since, you fix the conflict as in a merge. The stash then stays on the pile until you drop it.
A stash lives only on your computer. It is never pushed, and nobody else can see it. It also has no clear name unless you give one with git stash push -m and a message. A stash left for a month is a puzzle, because you will not remember what was in it.
A tag is a fixed name on one commit. It usually marks a release, such as v1.0 or v2.3.1. Unlike a branch, a tag never moves when new commits arrive. Years later, you can check out v1.0 and see the code exactly as it was shipped.
Release names often follow semantic versioning: three numbers, called major, minor and patch. In 2.3.1, the major version is 2, the minor is 3 and the patch is 1. A change that breaks old code bumps the first number. A new feature bumps the second, and a bug fix bumps the third.
There are two kinds of tag. A lightweight tag is only a name. An annotated tag, made with git tag -a, also stores who made it, when, and a message. Most teams use annotated tags for releases, because they record who shipped what.
The usual mistake is forgetting that a plain git push does not send tags. The tag sits on your computer, and nobody else sees the release. Push one tag by naming it, as in git push origin v1.0, or push all of them with git push --tags.
$ git stash
Saved working directory and index state WIP on main: 0daa45d Blue title
$ git stash list
stash@{0}: WIP on main: 0daa45d Blue title
$ git tag -a v1.0 -m "First release"
$ git tag
v1.0Worked example
The words for this lesson3
Check yourself
Why can my team not see the tag I just made?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.