Linux and Git commands · Lesson 20 of 23
stash, cherry-pick and tags
Put half-finished work aside with stash, copy a single commit to another branch with cherry-pick, and mark releases with tags.
On this page
Three small commands solve three very common problems. git stash parks work you are not ready to commit. git cherry-pick copies one commit onto another branch. git tag gives a permanent name to a commit, usually a release. They are simple tools, but each has a few sharp edges worth knowing.
Why this matters#
You are halfway through a feature when production breaks and you must switch to a hotfix branch right now. A hotfix is written on main, and you also need it on the release/1.0 branch that is currently deployed. And when someone asks "what exactly is running on the server?", you want the answer to be v1.0.0, not a long hash. Stash, cherry-pick and tags answer each of those.
All output comes from small practice repositories created for this lesson.
stash: park your work#
A stash saves your uncommitted changes on a stack and restores the working tree to the last commit. You then switch branches freely, and later bring the changes back.
We edited app.js and created a new file draft.txt:
git status -s
git stash
git status -s
git stash list M app.js
?? draft.txt
Saved working directory and index state WIP on main: 6eebe4e Initial app
?? draft.txt
stash@{0}: WIP on main: 6eebe4e Initial appThe edit to app.js was stashed, and app.js is back to its committed content. But draft.txt is still there: by default, stash only saves changes to tracked files. Add -u (--include-untracked) to stash new files as well, and give the stash a name with -m:
git stash -u -m "wip: draft"
git status -s | wc -l
git stash listSaved working directory and index state On main: wip: draft
0
stash@{0}: On main: wip: draftThe working tree is now completely clean. To bring the work back:
git stash poprestores the most recent stash and removes it from the stack.git stash applyrestores it but keeps it in the stack, which is useful when you want to apply the same changes to several branches.git stash dropdeletes a stash you no longer need.git stash show -pshows what a stash contains, as a patch.
git stash popDropped refs/stash@{0} (804695722426fa31b65863bbce1e3fda57d4d147)(The line above is the end of the output; before it Git printed the usual status of the restored files.)
You can stash several times, and the stack is numbered from stash@{0} (the newest). Name your stashes when you have more than one, or you will forget which is which.
A stash can be applied to a different branch from the one it was made on. If the files have changed in the meantime, popping can conflict:
Auto-merging app.js
CONFLICT (content): Merge conflict in app.jsWhen that happens, the stash is kept, not dropped, so nothing is lost. Resolve the conflict as in lesson 16, then run git stash drop yourself.
Stashes live only in your local repository. They are not pushed. Do not treat the stash as long-term storage: a branch with a commit is safer. If the work matters, make a WIP commit on a branch instead.
cherry-pick: copy one commit#
Sometimes you need one commit from another branch, not the whole branch. Our feature branch has three commits:
git log --oneline7f44ee2 Add f3
0f3f708 Add f2
8ace57e Add f1
5d75993 Initial appSuppose a bug fix lives on feature/x and must also go to the release/1.0 branch. Switch to the target and run git cherry-pick with the commit id. We use a fresh example with a branch that has a null-check fix:
git switch release/1.0
git cherry-pick -x 37f7eaa[release/1.0 3664dee] Fix null check in utilCherry-pick applies the change made by that commit as a new commit on your current branch. The new commit has a different hash, 3664dee, because it has a different parent. The graph shows two separate commits with the same message:
* c5da008 Reports: add CSV export
* 37f7eaa Fix null check in util
* 0640b13 Add reports module
| * 3664dee Fix null check in util
|/
* 44bf8cb Initial appThe -x option appends a line to the new commit's message saying where it came from, which is very useful later:
Fix null check in util
(cherry picked from commit 37f7eaade4f8c4341a2888d6f8006a17ae5a8d45)You can copy a range of commits. The range A..B means "after A, up to and including B", so this takes the last two commits of the feature branch:
git cherry-pick feature/x~2..feature/x
git log --oneline8283e9d Add f3
3777ae5 Add f2
5d75993 Initial appOther options: -n (--no-commit) applies the change to your working tree without committing, so you can combine several picks into one commit.
Cherry-pick can conflict, just like a merge, because the change has to apply to different surrounding code:
Auto-merging util.js
CONFLICT (content): Merge conflict in util.jsResolve it, git add the file and run git cherry-pick --continue. Or back out with git cherry-pick --abort. While a cherry-pick is unfinished, Git refuses to switch branches ("cannot switch branch while cherry-picking"), so finish or abort first.
Use cherry-pick sparingly. Because the copy is a separate commit, the same change now appears twice in history. If you later merge the two branches, Git usually copes, but it can produce confusing conflicts. It is the right tool for hotfixes and backports, and the wrong one for moving a whole feature, where merge or rebase is better.
tags: permanent names for commits#
A branch label moves every time you commit. A tag is a label that stays on one commit forever. Teams use tags to mark releases: v1.0.0 always means the same code.
There are two kinds.
- A lightweight tag is just a name pointing at a commit.
- An annotated tag is a real object that also stores a tagger, a date and a message. Use these for releases.
git tag v0.1.0
git tag -a v1.0.0 -m "First stable release"
git tag
git tag -nv0.1.0
v1.0.0
v0.1.0 Add f3
v1.0.0 First stable release-a makes the tag annotated, and -m gives its message. git tag -n shows the first line of each tag's message; for a lightweight tag that is simply the commit's message. We can see the difference at the object level:
git cat-file -t v0.1.0
git cat-file -t v1.0.0commit
tagA tag normally marks the commit you are on. To tag an older commit, add its hash at the end: git tag -a v0.9.0 -m "Beta" 3a4f2c1.
Semantic versioning is a common naming scheme, vMAJOR.MINOR.PATCH, where a breaking change raises the major number, a new feature raises the minor and a bug fix raises the patch. You do not have to use it, but pick one convention and keep it.
Finding your place relative to tags#
git describe --tags names the current commit using the latest tag:
git describe --tags
echo more > g.txt && git add . && git commit -m "After tag"
git describe --tagsv1.0.0
v1.0.0-1-ga19d9d1The second result means "one commit after v1.0.0, at commit a19d9d1" (the g stands for git). A build script can use this as a version string, so a server can always tell you exactly which code it runs.
You can look at the code as it was at a tag. Checking it out gives a detached HEAD (lesson 15), so create a branch if you plan to change anything:
git switch --detach v1.0.0HEAD detached at v1.0.0Tags and remotes#
Tags are not pushed by a normal git push. Push one explicitly, or push them all:
git push origin v1.0.0
git push origin --tags * [new tag] v1.0.0 -> v1.0.0
* [new tag] v0.1.0 -> v0.1.0Delete a tag locally with git tag -d v0.1.0 and on the remote with git push origin --delete v0.1.0. Git will not let you silently reuse a name:
fatal: tag 'v1.0.0' already existsTreat published tags as permanent. If a release is wrong, create a new tag, v1.0.1. Moving a tag that others have already downloaded causes the same kind of confusion as rewriting shared commits. In the capstone lesson, a tag will tell the server which version to deploy.
Try it#
- Edit a tracked file and create a new untracked one. Run
git stash, checkgit status -s, and notice what is left. Rungit stash pop, then repeat withgit stash -u -m "my note". - Create two stashes with different messages, view
git stash list, and apply the older one withgit stash apply stash@{1}. - Create a branch with three commits, switch to another branch and cherry-pick the middle one with
-x. Read the new commit message and the graph. - Cherry-pick a range of two commits onto another branch. Then make a conflicting cherry-pick and abort it with
git cherry-pick --abort. - Create a lightweight and an annotated tag. Compare them with
git cat-file -t, rungit describe --tagsbefore and after a new commit, and push the tags to a bare repository.
Sign in to keep track of the lessons you finish.