Git branching and tagging best practices

I am currently learning to use Git by reading Pro Git. Right now I'm learning about branching and tags. My question is when should I use a branch and when should I use a tag?

For example, say I create a branch for version 1.1 of a project. When I finish and release this version, should I leave the branch to mark the release version? Or should I add a tag? If I add a tag, should I delete the version branch (assuming that it is merged into master or some other branch)?

In short: Best practice is branch out, merge often and keep always in sync.

There are pretty clear conventions about keeping your code in a separate branches from master branch:

  1. You are about to make an implementation of major or disruptive change
  2. You are about to make some changes that might not be used
  3. You want to experiment on something that you are not sure it will work
  4. When you are told to branch out, others might have something they need to do in master

Rule of thumb is after branching out, you should keep in sync with the master branch. Because eventually you need to merge it back to master. In order to avoid a huge complicated mess of conflicts when merging back, you should commit often, merge often.

Good practices to follow

A successful Git branching model by Vincent Driessen has good suggestions. If this branching model appeals to you consider the flow extension to git. Others have commented about flow.

Tagging practices

As you already know, Git gives you commit identifiers like 1.0-2-g1ab3183 but those are not tags! Tagging is done with git tag, and the tags that are created using git tag are the base for the commit identifiers git describe creates. In another words, in Git you don't tag branches. You are tagging commits. It is correct to say that tag is just an annotated pointer to a commit.

Lets look at practical example that demonstrated it,

                        /-- [v1.0]
v
---.---.---.---S---.---A <-- master
\
\-.---B <-- test

Let's commit 'S' be commit pointed by tag 'v1.0'. This commit is both on branch 'master' and on branch 'test'. If you run "git describe" on top of commit 'A' (top of 'master' branch) you would get something like v1.0-2-g9c116e9. If you run "git describe" on top of commit 'A' ( aka the 'test' branch) you would get something like v1.0-2-g3f55e41 , that is the case with default git-describe configuration. Note that this result is slightly different. v1.0-2-g9c116e9 means that we are at commit with sortened SHA-1 id of 9c116e9, 2 commits after tag v1.0. There is no tag v1.0-2!

If you want your tag to appear only on branch 'master', you can create new commit (e.g. only update default / fallback version information in GIT-VERSION-FILE) after branching point of 'test' branch. If you tag commits on 'test' branch with e.g. 'v1.0.3` it would be visible only from 'test'.

References

I have found many, many, useful blogs and posts to learn from. However, the ones that are professionally illustrated are rare ones. Thus, I would like to recommend a post - A successful Git branching model by @nvie. I have borrowed his illustration :)

answered Sep 21 '12 at 18:57
EL Yusubov

19.6k53868
  • 2
    1.0-2-g1ab3183 is an identifier constructed by git describe from information available from git, but calling it a git identifier is a little too much. Git identifies by SHA hash; tags and branches are human constructs that git helpfully keeps track of. As such, make a tag when you think some human will one day wish to find a convenient bookmark to a commit. – mabraham Apr 21 '14 at 23:52

最新文章

  1. Linux虚拟机添加新硬盘的全程图解
  2. 页面无法加载main.css
  3. Bootstrap 巨幕页头缩略图和警告框组件
  4. iOS - (TableView中利用系统的 cell 设置 cell.textlabel 位置和大小)
  5. IIS7.5真变态,服务器时间格式导致不生成WebResource.axd
  6. php 彩票类 lottery
  7. UrlRewriter.dll伪静态实现二级域名泛解析
  8. spring4+hibernate3
  9. PAT-乙级-1004. 成绩排名 (20)
  10. Android 显示YUV编码格式
  11. BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )
  12. ubuntu phone/touch的源码从哪里下载?
  13. MPSOC之7——开发流程uramdisk
  14. centos6.8 安装gitlab记录
  15. CSS常用属性计算原理
  16. Linux允许、禁止ping包
  17. ActivityManagerService启动过程分析
  18. &lt;html&gt; ---- position
  19. gtest 三种事件机制
  20. ssm项目跨域访问

热门文章

  1. 【转】kubernetes 中 deployment 支持哪些键值
  2. Java数组的十大方法
  3. apache 把404页面的url转发给php脚本处理
  4. BZOJ 3261: 最大异或和位置-贪心+可持久化01Trie树
  5. CodeForces 723E One-Way Reform
  6. 使用appframework前端框架中输入框圆角问题
  7. 配置无线AP 采用POE供电模块怎么配置无线AP没有POE交换机
  8. 【AC自动机】【状压dp】【滚动数组】hdu6086 Rikka with String
  9. JDK源码学习笔记——LinkedList
  10. 将字符串的编码格式转换为utf-8