Add new commit to the existing Git tag

孤街浪徒 提交于 2019-12-03 08:24:29

问题


I have created a Git tag as v1.1 using

git tag -a v1.1 -m 'my version 1.1'

and I pushed that tag. Later, I made some changes related to v1.1. Now when I push new changes and check the git tag using git describe it is showing me v1.1-g2dcc97.

How can I add my new commit to the existing tag?


回答1:


You can't put a new commit into an existing tag without breaking an important Git guideline: Never(*) modify commits that you have published.

Tags in Git aren't meant to be mutable. Once you push a tag out there, leave it alone.

You can, however, add some changes on top of v1.1 and release something like v1.1.1 or v1.2. One way of doing that would be

# Create a new branch from tag v1.1
git checkout -b newbranch v1.1

# Do some work and commit it

# Create a new tag from your work
git tag -a -m "Tag version 1.1.1, a bugfix release" v1.1.1

(*) Unless you have a really super special reason for doing so, and only if you completely understand the implications, and even then, don't make a habit of it.




回答2:


If you absolutely need to "move" the tag instead of creating a new one, You can do this:

NB: As @Chris said, make sure you have a good reason for not wanting to create a new tag because the best practice is to create a new one

1. Checkout the tag (a Detached HEAD)

git checkout tag/v1.1

2. Create and Checkout a branch off that tag (i.e. Branching off the tag)

git checkout -b my-tagged-branch

*** do work and commit changes ***

3. Push to the remote branch.

git push  -u origin my-tagged-branch

If needed merge branch into other branches that need the change (in case of a bug fix for example)

4. While still on my-tagged-branch, Delete the tag

git tag -d v1.1

5. Create the tag again: This will "move" the tag to point to your latest commit on that branch

git tag v1.1

6. Delete the tag on remote

git push origin :v1.1

7. Create the tag on remote

git push origin v1.1



来源:https://stackoverflow.com/questions/21459540/add-new-commit-to-the-existing-git-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!