Ignoring files, tags & releases
Every real repository needs two things: keeping the wrong files out (build output, dependencies, secrets) and marking the commits that became releases. This chapter covers the .gitignore rules and traps, then how tags turn into versioned releases on GitHub.
.gitignore patterns
Patterns are matched relative to the folder the .gitignore is in:
| Pattern | Matches |
|---|---|
*.log | Any .log file, in any folder |
.env | A file or folder named .env anywhere |
build/ | Folders named build anywhere (trailing slash = folders only) |
/build | Only build at the top level (leading slash anchors it) |
docs/*.pdf | PDFs directly in top-level docs/, not docs/old/ (* never crosses a /) |
**/logs | logs at any depth |
docs/**/*.pdf | PDFs anywhere under docs/ |
!keep.log | Negation: un-ignore what an earlier line ignored |
# text | A comment (\# for a name that really starts with #) |
# dependencies and build output
node_modules/
.venv/
__pycache__/
dist/
# logs and OS junk
*.log
.DS_Store
# secrets: ignore every .env variant, but share the template
.env
.env.*
!.env.example
logs/, the line !logs/keep.log does nothing, because Git never looks inside. Use logs/* then !logs/keep.log.Three places for ignore rules
.gitignore: committed and shared. Rules about the project..git/info/exclude: same syntax, never committed. For this clone only.- A global file for your machine: editor and OS files (
.idea/,.vscode/,.DS_Store) that aren't the project's business.
$ git config --global core.excludesFile ~/.gitignore_global
# (if unset, Git already reads ~/.config/git/ignore)
$ git check-ignore -v debug.log # which rule ignores this file?
.gitignore:8:*.log debug.log
$ git status --ignored # show ignored files too
$ git add -f dist/keep-me.js # force-add one ignored file
Untracking an already committed file
.gitignore only affects untracked files. If .env or node_modules/ was committed before the rule existed, Git keeps tracking it. Remove it from the index but keep it on disk:
$ echo ".env" >> .gitignore
$ git rm --cached .env # stop tracking, keep the local file
$ git rm -r --cached node_modules # -r for a folder
$ git commit -m "Stop tracking .env and node_modules"
$ git rm -r --cached . && git add . # re-apply ALL ignore rules at once
- When teammates pull this, Git deletes the file from their working folders. Warn them to back up their
.env. - The file is still in every old commit. If it held a secret, rotate the secret; scrubbing history needs
git filter-repoand a force-push, and the secret should still be treated as leaked.
Empty folders and .gitkeep
Git tracks files, not folders, so an empty folder can't be committed. The convention is an empty .gitkeep file inside it; the name means nothing to Git. To keep a folder but ignore its future contents (uploads/, logs/), put a .gitignore in it containing * and !.gitignore.
Lightweight vs annotated tags
- Lightweight: just a name for a commit.
git tag v1.0.0 - Annotated: a stored object with tagger, date and message, optionally signed (
-s).git tag -a v1.0.0 -m "First stable release"
Use annotated tags for releases: they record who released what and when, and git describe and --follow-tags only consider annotated tags by default.
$ git tag -a v1.2.0 -m "Release 1.2.0" # tag the current commit
$ git tag -a v1.1.1 3955422 -m "Hotfix 1.1.1" # tag an older commit
$ git tag -l "v1.*" # list matching tags
$ git show v1.2.0 # tag details + the commit
$ git push origin v1.2.0 # push one tag (plain git push sends none)
$ git push --follow-tags # commits + the annotated tags pointing at them
$ git push --tags # every local tag, lightweight included
$ git tag -d v1.2.0 # delete locally...
$ git push origin --delete v1.2.0 # ...and on GitHub
v1.2.1 instead.Semantic versioning
SemVer versions look like MAJOR.MINOR.PATCH:
- MAJOR for breaking changes (removed endpoint, renamed option):
2.4.1→3.0.0. - MINOR for new, backwards-compatible features:
2.4.1→2.5.0. - PATCH for backwards-compatible fixes:
2.4.1→2.4.2.
Bumping a number resets those to its right. Pre-releases like 3.0.0-rc.1 sort before 3.0.0, and 0.y.z means "anything may change". The v in v2.4.1 is a tag-name convention, not part of the version. It lets users judge upgrades from the number alone, and ranges like npm's ^2.4.1 (any 2.x from 2.4.1 up) rely on it.
git describe
$ git describe
v1.2.0-5-g3f9a1c2 # 5 commits after v1.2.0; "g" + short hash of HEAD
$ git describe --dirty
v1.2.0-5-g3f9a1c2-dirty # ...with uncommitted changes
$ git describe --abbrev=0
v1.2.0 # just the latest tag
Exactly on a tagged commit it prints just v1.2.0, which makes it a cheap build version stamp. With no tags it fails with "No names found, cannot describe anything"; with only lightweight tags it says "No annotated tags can describe…" (add --tags, or --always to fall back to a hash).
GitHub Releases
A release is a GitHub page built on a tag, with a title, notes and downloadable files. GitHub adds source archives automatically and can generate release notes from the pull requests merged since the last release. On the website: Releases → Draft a new release. Or with gh:
$ gh release create v1.2.0 --generate-notes
$ gh release create v1.2.0 dist/app-linux-amd64.tar.gz --title "v1.2.0" --notes-file CHANGELOG.md
$ gh release create v2.0.0-rc.1 --prerelease --generate-notes
If the tag doesn't exist yet, gh creates it from the default branch's latest commit (--verify-tag refuses instead). In CI, pushing a version tag is a common release trigger:
# .github/workflows/release.yml
name: Release
on:
push:
tags: ["v*.*.*"]
permissions:
contents: write # needed to create the release
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: gh release create "$GITHUB_REF_NAME" --generate-notes
env:
GH_TOKEN: ${{ github.token }}
Releasing is then: merge to main, git tag -a v1.3.0 -m "Release 1.3.0", git push origin v1.3.0, and the pipeline does the rest.