git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Suggestion: error "tag ... already exists" should distinguish between tagging different or same commit:

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 10, 2025, 14:51 UTC
Message-ID
<xmqqzfefodje.fsf@gitster.g>
In-Reply-To
<CAGJzqskj803dUcEuV+P-yuWdT0tiaidb7h3YxQSCgYHWgBfaWA@mail.gmail.com>
M Hickford <mirth.hickford@gmail.com> writes:
Show 15 quoted lines
>> Besides, in the above examples, is d4e6038a something the user
>> immediately recognises as the same as v1.9.5 or the object existing
>> v1.9.5 tag points at?  I somehow doubt it.  So after getting the
>> error, there needs some digging to figure out how v1.9.5 and
>> existing hello are related to each other _anyway_, I would think.
>
> Good point. How about just changing the second error message?
>
>>git tag hello v1.9.5
>
>>git tag hello v1.9.5
> fatal: tag 'hello' already exists
>
>>git tag hello v2.0.0
> fatal: tag 'hello' already exists but points at a different commit

Or simply something like this. I am not convinced (yet) that this is a good idea; I merely is showing that the implementation would look like this.

----- >8 -----
Subject: tag: allow idempotent "git tag" without "--force"

When "git tag T O" is told to create a tag pointing at an object O without the "--force" option, it refuses with "tag T already exists", even when T points at O (which makes it a no-op).

Let's allow this "idempotent" case by special casing.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/tag.c  |  2 +-
 t/t7004-tag.sh | 12 +++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)
diff --git c/builtin/tag.c w/builtin/tag.c
index 4742b27d16..5380a46494 100644
--- c/builtin/tag.c
+++ w/builtin/tag.c
@@ -660,7 +660,7 @@ int cmd_tag(int argc,
 
 	if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev))
 		oidclr(&prev, the_repository->hash_algo);
-	else if (!force)
+	else if (!force && (create_tag_object || !oideq(&object, &prev)))
 		die(_("tag '%s' already exists"), tag);
 
 	opt.message_given = msg.given || msgfile;
diff --git c/t/t7004-tag.sh w/t/t7004-tag.sh
index 10835631ca..9a253a44a8 100755
--- c/t/t7004-tag.sh
+++ w/t/t7004-tag.sh
@@ -126,7 +126,7 @@ test_expect_success 'annotated tag with --create-reflog has correct message' '
 '
 
 test_expect_success '--create-reflog does not create reflog on failure' '
-	test_must_fail git tag --create-reflog mytag &&
+	test_must_fail git tag --create-reflog mytag no-such-object &&
 	test_must_fail git reflog exists refs/tags/mytag
 '
 
@@ -183,8 +183,14 @@ test_expect_success 'listing tags using a non-matching pattern should output not
 
 # special cases for creating tags:
 
-test_expect_success 'trying to create a tag with the name of one existing should fail' '
-	test_must_fail git tag mytag
+test_expect_success 'recreating a tag without --force' '
+	# light-weight tag pointing at the same thing
+	# now succeeds
+	git tag mytag HEAD &&
+	# light-weight tag pointing at a different thing
+	test_must_fail git tag mytag HEAD: &&
+	# creating annotated tag, pointing at the same object.
+	test_must_fail git tag -a -m anno mytag $taggedobject
 '
 
 test_expect_success 'trying to create a tag with a non-valid name should fail' '
Previous: M HickfordNext: Junio C Hamano
Message 8 of 14 in “Suggestion: error "tag ... already exists" should distinguish between tagging different or same commit:”
  1. M HickfordJun 9, 2025
  2. Junio C HamanoJun 9, 2025
  3. Hilco WijbengaJun 9, 2025
  4. Andreas SchwabJun 9, 2025
  5. Junio C HamanoJun 9, 2025
  6. Hilco WijbengaJun 9, 2025
  7. M HickfordJun 10, 2025
  8. Junio C HamanoJun 10, 2025
  9. Junio C HamanoJul 7, 2025
  10. rsbecker@nexbridge.comJul 7, 2025
  11. Junio C HamanoJul 11, 2025
  12. tag: allow idempotent "git tag" without "--force"Junio C Hamano, Jul 11, 2025
  13. Justin ToblerJul 11, 2025
  14. Junio C HamanoJul 11, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.