Git Tags

1 min

Índice

  1. O que é um Tag?
  2. Tipos de Tags
  3. Utilização prática
  4. Gestão das versões

1. O que é um Tag?

Git tag = etiqueta permanente num commit específico.

Uso principal: marcar as versões (v1.0, v2.0, etc.)

Diferença em relação às branches:

  • Branch = móvel, evolui com novos commits
  • Tag = fixo, aponta sempre para o mesmo commit

Analogia: Tag = autocolante « Versão 1.0 » numa página do caderno.

Voltar ao índice

2. Tipos de Tags

Lightweight Tag (simples)

bash
git tag v1.0                # Tag simples
  • Só um nome
  • Ponteiro para um commit
  • Rápido de criar

Annotated Tag (completo)

bash
git tag -a v1.0 -m "Version 1.0 - First stable release"
  • Contém uma mensagem
  • Data e autor
  • Pode ser assinado (GPG)
  • Recomendado para releases oficiais

Voltar ao índice

3. Utilização prática

Setup do projeto:

bash
mkdir demo-tags && cd demo-tags
git init

echo "print('app v0.1')" > app.py
git add . && git commit -m "Initial version"

echo "print('app v0.2')" > app.py  
git add . && git commit -m "Bug fixes"

echo "print('app v1.0')" > app.py
git add . && git commit -m "First stable version"

Criar tags:

bash
# Tag simples
git tag v1.0

# Tag com mensagem  
git tag -a v1.0-stable -m "Version 1.0 - Production ready"

# Tag num commit anterior
git log --oneline           # Ver os hashes
git tag v0.2-beta <hash>    # Tag num commit específico

Listar tags:

bash
git tag                     # Todas as tags
git tag -l "v1.*"          # Tags que correspondem ao padrão
git show v1.0              # Detalhes da tag
bash
git checkout v1.0          # Ir ao commit etiquetado
git checkout main          # Voltar a main

Push das tags:

bash
git push origin v1.0       # Push de uma tag específica
git push origin --tags     # Push de todas as tags

Voltar ao índice

4. Gestão das versões

Convenção Semantic Versioning:

vMAJOR.MINOR.PATCH
- v1.0.0 → First stable release
- v1.0.1 → Bug fix
- v1.1.0 → New feature (backward compatible)  
- v2.0.0 → Breaking changes

Workflow de release típico:

bash
# Desenvolvimento concluído
git checkout main
git pull origin main

# Tag de release
git tag -a v1.2.0 -m "Release v1.2.0 - Add user authentication"

# Push do código + tags
git push origin main
git push origin v1.2.0

# Ou push tudo de uma vez:
git push origin main --tags

Gestão das tags:

bash
# Apagar tag local
git tag -d v1.0

# Apagar tag remota
git push origin --delete v1.0

# Modificar uma tag (recriar)
git tag -d v1.0                           # Apagar local
git push origin --delete v1.0             # Apagar remoto  
git tag -a v1.0 -m "Corrected message"    # Recriar
git push origin v1.0                      # Voltar a enviar

Casos de uso:

1. Release em produção:

bash
git tag -a v2.1.0 -m "Production release v2.1.0"
git push origin v2.1.0
# Implantar a versão v2.1.0 em produção

2. Voltar a uma versão anterior:

bash
git checkout v2.0.0        # Voltar à versão estável
git checkout -b hotfix-v2.0.0    # Criar branch hotfix

3. Comparar versões:

bash
git diff v1.0.0 v2.0.0     # Ver todas as alterações

Tags = marcos permanentes do seu projeto. Use-as em todas as releases!

Voltar ao índice