Names That Do Not Move
A branch name moves forward with every commit. Sometimes you need a name that
stays put: "the version we released", "what we sent to the printer". That is a
tag: a fixed name for one commit. Tags are commonly used for release
versions, such as v1.0.
Two Kinds of Tag
git tag -a v1.0 -m "First team handbook" # annotated tag
git tag draft-2 # lightweight tag
git tag # list tags
git show v1.0 # show a tag and its commit
- An annotated tag (
-a) is stored as its own object with the tagger's name, a date and a message.git showdisplays those before the commit. Use annotated tags for releases and anything others rely on. - A lightweight tag is just a name pointing at a commit, with no message or tagger. It suits quick private bookmarks.
Without a commit name, a tag marks the commit you are on. Add a commit name or
ID at the end to tag another one. Each tag name can be used once; Git refuses a
second v1.0 with tag 'v1.0' already exists.
Tags Are Shared Separately
git push sends branches, not tags. A new tag stays in your clone until you
push it by name:
git push origin v1.0 # push one tag
git push origin --tags # push all your tags
On the other side, git fetch and git pull bring in tags that point at the
commits they download, reporting * [new tag].
Looking at a Tagged Version
To see the files as they were at a tag, switch to it with a detached HEAD:
git switch --detach v1.0
git status says HEAD detached at v1.0. You can look around and run the
project as it was. To go back, switch to a branch. Plain git switch v1.0
refuses, because a tag is not a branch; older guides use git checkout v1.0,
which detaches in the same way.
Exercise: Publish and Use a Release Tag
Asha marks the current team handbook as version 1.0 and shares the tag. Ravi fetches it and later looks back at that version.
Starting state: Git configured as in the configuration lesson. You will rebuild the team practice set.
-
Rebuild the practice set:
cd ~/git-practice rm -rf team mkdir team cd team git init --bare -b main shared.git git clone shared.git asha cd asha git config user.name "Asha Rao" git config user.email "asha@example.com" cat > README.md <<'EOF' # Team notes We meet on Mondays. Bring your laptop. EOF git add README.md git commit -m "Add team notes" git push -u origin main cd .. git clone shared.git ravi cd ravi git config user.name "Ravi Kumar" git config user.email "ravi@example.com" cd .. -
As Asha, tag the current commit and look at the tag:
cd asha git tag -a v1.0 -m "First team handbook" git tag git show v1.0git taglistsv1.0.git show v1.0starts withtag v1.0, aTagger: Asha Raoline, the date and the message, then shows the commit. -
Push the branch, and check whether the shared repository has the tag:
git push git ls-remote --tags origingit pushsaysEverything up-to-date, andgit ls-remote --tags origin, which lists the tags in the remote, prints nothing: the tag was not shared. Push it by name:git push origin v1.0Git reports
* [new tag] v1.0 -> v1.0. -
As Ravi, fetch and list tags:
cd ../ravi git fetch git tagThe fetch reports
* [new tag]forv1.0, andgit taglists it. -
The project moves on. Asha adds a line and shares it, and Ravi pulls:
cd ../asha cat >> README.md <<'EOF' Room 4 is booked for us. EOF git commit -am "Add meeting room" git push cd ../ravi git pull -
As Ravi, look at version 1.0, then return:
git switch --detach v1.0 git status cat README.md git switch maingit statussaysHEAD detached at v1.0, andREADME.mdhas no line about Room 4: it is the version Asha tagged. Aftergit switch main, the newest version is back.
Success: Ravi saw v1.0 after fetching and could switch to the tagged
version.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| The tag is missing in Ravi's clone | The tag was never pushed | git push origin v1.0 in Asha's clone, then fetch again |
fatal: a branch is expected, got tag 'v1.0' | git switch needs --detach for a tag | git switch --detach v1.0 |
tag 'v1.0' already exists | The name is taken | Choose a new name, such as v1.1 |
git show has no Tagger: line | It is a lightweight tag | Create releases with git tag -a |
- Keep the practice set for the next lesson.
Independent task: as Asha, mark the newest commit with a lightweight tag
named draft-2 and share it. As Ravi, fetch it, then compare what
git show draft-2 prints with git show v1.0.
Check Your Understanding
- Asha created
v1.1and rangit push, which said everything was up to date. Ravi fetches but sees nov1.1. Why? - Why do teams prefer annotated tags for releases?
Answer and Reasoning
Independent task
git tag draft-2
git show draft-2
git push origin draft-2
cd ../ravi
git fetch
git tag
Run the first three commands in asha. Ravi's git tag lists draft-2 and
v1.0. git show draft-2 begins straight away with the commit, because a
lightweight tag has no tagger, date or message of its own. git show v1.0
first shows tag v1.0, the tagger, the date and "First team handbook".
Understanding check
git pushsends branches, not tags, sov1.1never reached the shared repository. Asha needsgit push origin v1.1, orgit push origin --tags.- An annotated tag records who made it, when and why, which matters for a version other people depend on. A lightweight tag is only a name.