159

I've been using git for about a year and would like to use tagging to, well, tag commits at different versions. I've found lots of info on the commands to use to work with tags, but what I'd like to know is why use tagging at all if I can just create a new branch called 1.1.0 and not have to cloud my mind with a whole new set of git commands?

There has to be a lot of good reasons for tagging rather than branching but I'd like to know what those advantages are.

6 Answers 6

195

A tag is immutable.

Whereas you can create a branch named "1.0.0" - you, or anyone with commit rights, can also then simply push to that branch (deliberately or not) and change what 1.0.0 means.

You can't do that with a tag, once you create a tag - that's it; Tag 1.0.0 means exactly that and can't be changed*.

That's the main practical difference between a tag and a branch

* You can delete and recreate a tag thereby changing a tag, but certainly not by accident.

123

Tags are mainly used for future reference to the specific version of the project, by tagging a commit. You can always use branches of course, but if you change versions a lot, you will end up with lots of unused or rarely used branches.

Practically, tags are branches without branches anyway, just adding a way to reference a specific version of the project to reduce complexity.

Edit: Here is a nice way to use git that I use for all my projects.

5
  • 1
    Heh(: It's really a great workflow that covers all the possible solutions. Commented Mar 21, 2012 at 18:03
  • yes, I've seen the nvie method before and have been quite befuddled by it. Nonetheless, I aspire to implement it once I understand it. I guess with a tag, you cannot accidentally change the code, commit, and still be at the same version. With branches, it may inadvertently happen. Tags seem to be a safer way of marking releases.
    – wufoo
    Commented Mar 21, 2012 at 18:13
  • 2
    The beauty of the nvie method for me was that I didn't need to understand it initially. I could just find the section for what I wanted to do and type out the commands. After a few times it became naturally and withing a few days I was dancing around branches like a pro!
    – Killroy
    Commented May 1, 2015 at 17:47
  • Hoping to understand the gitflow approach by just blindly applying it will make you miss all the simplifications that you could apply to it for your situation. And gitflow is indeed inappropriate for most teams: either overly complex, or overly simple.
    – toolforger
    Commented Jun 18, 2018 at 14:09
  • 1
    I'd give this a +1 for the general notion, if it wasn't for the edit. In 2020, you might not want to strictly follow git-flow, if you are in the position to pick a branching model. Minimum employing a develop branch isn't common practice anymore, for a good reason too, as it has very little merit in a rapid release cycle setup with proper pipelines in place. Commented Dec 9, 2020 at 10:33
33

Branch and tag are the same thing (pointer to a commit, aka. "ref"), except branch automatically moves to the next commit while tag stays forever1 on the same commit.

When making a release, you generally want to mark the "snapshot" of the code from which that release was built, and you want it to stay marked that way even as you continue to evolve the code, so you'd use a tag.

If you tried using a branch for that, it could inadvertently move to a different commit, from which the release was not built.


1 Unless you delete the tag, of course.

NOTE: I realize this is an old question, but I felt that the similarity (and one crucial difference) between branches and tags has not been fleshed out in other answers as clearly as it could have been.

4
  • Dear @downvoter, any specific reason for the downvote? Commented Nov 7, 2016 at 22:12
  • 1
    I didn’t downvote your answer but what do you mean by “branch automatically moves to the next commit”? Branches don’t automatically move to any commit. Running git commit updates the checked out branch’s head to reference the new commit, yes, but no other branch “automatically” moves to the next commit. You ought to clarify the first paragraph of your answer.
    – Toothbrush
    Commented Dec 6, 2018 at 3:08
  • 1
    @Toothbrush Sure, that's what I meant by "automatically moves". However, branch doesn't really "own" any commits, it doesn't even point to a set of commits, it just points to one commit (and the rest can be implied, sometimes imprecisely, by walking the commit graph). Under git, branch is just a one-line file under .git\refs\heads, containing the commit's hash. Commits themselves don't "remember" which branch created them. This is different from Mercurial, for example, where the branch info can be written into the commit's metadata. Commented Dec 6, 2018 at 13:24
  • Yes, but a lot of people don't know that — especially newcomers who are confused about the myriad of ways that are suggested online of doing things with Git.
    – Toothbrush
    Commented Dec 6, 2018 at 14:29
19

I tend to use a workflow that incorporates both tags and branches. Tags are good for marking released code or notable development builds. Branches are good for keeping track of all changes relevant to a specific version.

Here's a good writeup on this type of workflow: http://nvie.com/posts/a-successful-git-branching-model/

9

In addition to the other answers, here is my 2 cents.

Short Answer: Use tags for release versions

Long Answer: I believe using tags for release versioning specifically is better than using branches. If you need to update the relase, simply branch off of the tagged commit and once you finish working on that branch (most likely a hotfix branch), create a new tag at the head of that new branch with the new version. Then, merge that branch back into master/develop because you really shouldn't be changing a release version unless it's a hotfix that likely should be merged back into your source code. Then delete that branch since it's no longer needed. If you need to apply another hotfix to that new version, repeat the same steps.

Refer to the section of the following article that shows how to merge a hotfix with the author's Git workflow - https://hackernoon.com/a-branching-and-releasing-strategy-that-fits-github-flow-be1b6c48eca2

7

You use tags to note important commits in history. "This was the exact commit we used for this version on that rainy thursday when the build server broke". If you use a branch instead of a tag, you can never know what exact commit you used. You only know "We released version 1.1.0 somewhere on this branch", unless you manually write down the exact hash for that commit, which is why you use tags in the first place :)

1
  • 5
    I think he meant creating a branch named 1.1.0 and not using it anymore, so it will represent the project in the named version. Commented Mar 21, 2012 at 18:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.