Git Cheatsheet

Tags

Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Tag Types

TypeDescriptionHas message/metadata?
LightweightJust a named pointer to a commitNo
AnnotatedFull Git object with tagger, date, message, optional GPG signatureYes

Prefer annotated tags for releases — they carry metadata and can be GPG-signed.

Creating Tags

# Lightweight tag
git tag <name>                  # Tag current HEAD
git tag <name> <sha>            # Tag a specific commit
git tag v1.0.0                  # Example: lightweight v1.0.0 at HEAD

# Annotated tag
git tag -a <name> -m "message"          # Annotated tag at HEAD
git tag -a <name> <sha> -m "message"    # Annotated tag at a specific commit
git tag -a v1.0.0 -m "Release v1.0.0"  # Example

# Signed tag (GPG)
git tag -s <name> -m "message"          # Sign with default GPG key
git tag -s v1.0.0 -m "Signed release"  # Example
git tag -u <key-id> <name> -m "msg"    # Sign with a specific GPG key

Listing Tags

git tag                         # List all tags alphabetically
git tag -l                      # Same (explicit)
git tag -l "v1.*"               # Filter by pattern (glob)
git tag -l "v2.0.*"             # Filter to a minor version
git tag --sort=version:refname  # Sort by semantic version
git tag --sort=-version:refname # Sort descending (latest first)
git tag --sort=-creatordate     # Sort by date (most recent first)
git tag -n                      # Show first line of tag message
git tag -n5                     # Show first 5 lines of tag message
git tag --contains <sha>        # List tags that contain a specific commit

Inspecting Tags

git show <tag>                  # Show tag metadata + the tagged commit
git show v1.0.0                 # Example
git cat-file -p <tag>           # Show raw tag object
git rev-parse v1.0.0            # Show the SHA of the tag object (annotated)
git rev-parse v1.0.0^{}         # Dereference to the commit SHA
git log v1.0.0 -1               # Show only the tagged commit

Pushing Tags to Remote

git push origin <tag>           # Push a single tag
git push origin v1.0.0          # Example
git push --tags                 # Push ALL local tags to remote
git push origin --tags          # Same, explicit remote
git push --follow-tags          # Push commits + annotated tags reachable from the pushed commits

--follow-tags is usually the right default for release workflows — it only pushes annotated tags, not lightweight ones.

Deleting Tags

# Delete locally
git tag -d <name>               # Delete a local tag
git tag -d v1.0.0 v1.0.1       # Delete multiple tags

# Delete from remote
git push origin --delete <name>     # Delete a remote tag
git push origin :refs/tags/<name>   # Older syntax (same effect)
git push origin --delete v1.0.0     # Example

Checking Out a Tag

Checking out a tag puts you in detached HEAD state:

git checkout v1.0.0             # Detached HEAD at the tagged commit
git switch --detach v1.0.0      # Modern equivalent

# If you want to make commits from this point:
git checkout -b release/v1.0.0 v1.0.0  # Create a branch from the tag
git switch -c release/v1.0.0 v1.0.0    # Modern equivalent

Verifying Signed Tags

git tag -v <name>               # Verify a GPG-signed tag
git tag -v v1.0.0               # Example

Re-Tagging (Moving a Tag)

Tags are immutable by convention, but you can force-overwrite:

git tag -f v1.0.0 <sha>         # Move tag to a new commit (local)
git push --force origin v1.0.0  # Force-push updated tag (requires coordination)

Moving a published tag breaks everyone who has already fetched it. Communicate clearly or create a new tag instead (e.g., v1.0.1).

Fetching Tags from Remote

git fetch --tags                # Fetch all tags
git fetch origin <tag>          # Fetch a specific tag
git fetch --no-tags             # Prevent auto-fetching tags

Semantic Versioning Convention

# Format: vMAJOR.MINOR.PATCH[-prerelease][+build]
git tag -a v1.0.0 -m "Initial stable release"
git tag -a v1.1.0 -m "Add feature X"
git tag -a v1.1.1 -m "Fix bug Y"
git tag -a v2.0.0 -m "Breaking API change"
git tag -a v2.0.0-rc.1 -m "Release candidate 1"
git tag -a v2.0.0-beta.2 -m "Beta 2"

Using Tags in Commands

git diff v1.0.0..v2.0.0         # Diff between two tagged releases
git log v1.0.0..v2.0.0 --oneline  # Changelog between releases
git log v1.0.0..HEAD            # Commits since last release
git shortlog v1.0.0..HEAD       # Grouped by author (good for release notes)
git describe                    # Describe HEAD relative to nearest tag
git describe --tags             # Include lightweight tags in describe
git describe --abbrev=0         # Show just the tag name (no commit count/sha)

git describe

git describe                    # e.g., "v1.0.0-14-gabcdef0" (tag-commits-sha)
git describe --tags             # Use any tag (including lightweight)
git describe --always           # Fall back to SHA if no tag found
git describe --match "v[0-9]*"  # Only match version tags
git describe HEAD~5             # Describe a specific commit

Output format: <tag>-<commits-since>-g<sha> - v1.0.0 — exactly on tag - v1.0.0-14-gabcdef0 — 14 commits after v1.0.0, at commit abcdef0