简体   繁体   中英

How to remove a tag from newer commits in git?

I created a new tag v1.0.0 and git tagged commits a , b , c , d with v1.0.0 . a, b, c, d are the commit ids:

d -> Newest
c
b
a -> Oldest

Here is an example repository: https://github.com/axelthat/git-test . See how each commits are tagged with v1.0.0. 在此处输入图像描述

But I want to tag only the commits a and b with v1.0.0 . How do I do this?

A git tag points at one commit

See how each commits are tagged with v1.0.0.

This phrase from the question is a misunderstanding or misrepresentation of the information github is displaying.

The commits a , b and c are part of tag 1.0.0 they are not "tagged with" 1.0.0 - this difference in terminology may seem pedantic, but is the root of the confusion in the question and comments (which can now all be deleted), a tag can only point at one commit.

An example to reinforce that, consider the following repo:

➜  git init
Initialized empty Git repository in /private/tmp/so/.git/
➜  echo "a" > README.md
➜  git add README.md
➜  git commit -m "a"
[main (root-commit) 3900dc2] a
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
➜  echo "b" > README.md
➜  git commit -am "b"
[main e5dbd9c] b
 1 file changed, 1 insertion(+), 1 deletion(-)
➜  echo "c" > README.md
➜  git commit -am "c"
[main 79d52b2] c
 1 file changed, 1 insertion(+), 1 deletion(-)
➜  echo "d" > README.md
➜  git commit -am "d"
[main 5d6e012] d
 1 file changed, 1 insertion(+), 1 deletion(-)
➜  

This has created a git repo with commits. If I now create a tag and look at the history:

➜  git tag v1.0.0
➜  git log --graph --decorate --oneline
* 5d6e012 (HEAD -> main, tag: v1.0.0) d
* 79d52b2 c
* e5dbd9c b
* 3900dc2 a

There are 4 commits and one of them ( d ) is also the tag 1.0.0 - the other commits are the ancestors of d , and the tag.

A tag is actually just a file - it's freely available to take a look at:

➜  cat .git/refs/tags/v1.0.0
5d6e012b898e87617a9e1d138cd072fa96053009

How to tag not-head?

I want to tag only the commits a and b with v1.0.0. How do I do this?

Rephrasing to match git terminology this is: "How to create tag v1.0.0 for commit b ?"

Which is pretty easy to do, all of this can be done via github's ui but I'll demonstrate here the cli steps:

  1. Delete the incorrect tag locally
➜  git tag -d v1.0.0
  1. Delete the incorrect tag on the remote
➜  git push --delete origin v1.0.0
  1. Create the correct tag locally
➜  git tag v1.0.0 e5dbd9c # the sha for commit "b"

Note the local state is now:

➜  git log --graph --decorate --oneline
* 5d6e012 (HEAD -> main) d
* 79d52b2 c
* e5dbd9c (tag: v1.0.0) b
* 3900dc2 a
  1. update remote with tags
➜  git push --tags

And done :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM