What are branches and tags?

A branch is a name that moves to each new commit; a tag is a name that stays put.

A branch is a name that points at a commit. That is all it is: not a copy of the files and not a folder, a name and a pointer. What makes it useful is that the pointer moves. When you commit on a branch, the branch moves to the new commit, so the name always means the newest commit on that line of work.

Because a branch is only a pointer, making one costs nothing, and there is no reason to be sparing with them. Two branches can point at the same commit, and until one of them moves they are one line of work under two names.

A tag is the other kind of name. It points at a commit too, but it does not move: v1.0 stays on the commit it was put on, however much history is added afterwards. That is what makes tags right for releases and wrong for work in progress.

  1. Three commits. main points at the newest, C, and HEAD points at main.

  2. A new branch, feature, made at C. It is a second name for the same commit; nothing has been copied.

  3. Switch to feature and commit. D points back at C, and feature moves to D. main is still at C.

  4. A tag, v1.0, on C. It will still be on C when both branches have moved on.

  5. Back on main, a commit E. The two branches have diverged: each has a commit the other does not, and C is the last one they share.

What deleting a branch deletes

The name. The commits stay in the repository, and as long as another branch or a tag can reach them they are as safe as they ever were. A commit that no name reaches is another matter: Git keeps it for a while, in case, and eventually throws it away. Revert and reset comes back to that.

On the command line

git branch feature       # a new branch at the current commit
git switch feature       # move HEAD to it
git tag v1.0             # a tag on the current commit
git branch -d feature    # delete the name; the commits stay

Git’s own reference: git-branch, git-switch and git-tag. The Pro Git book draws the same pictures.

In Innesta

The switcher, and what happens to uncommitted work when you use it, is on creating and switching branches. Tags covers the two kinds, lightweight and annotated; deleting and restoring covers the check Innesta runs before a name goes, and renaming is a short answer.

The Playground is a repository made for trying this on.