Skip to contentSkip to conversation

error: refs/remotes/origin/HEAD points nowhere!

PATCH In review Teemu Likonen 4 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi,

Very annoying error "error: refs/remotes/origin/HEAD points nowhere!" keeps popping up almost all the time after user has removed remote repository "origin". Here's a kind of real-life example:

I have cloned some remote repository:

git clone git://project/project.git

After a while I start tracking some other remote repository. I do this:

git remote add ng git://project-ng/project.git

Then I do "git fetch ng" and probably also "git checkout --track -b master-ng ng/master". Then I find the old "origin" useless to me so I remove it with "git remote rm origin".

It seems that refs/remotes/origin/HEAD is still left in my repository and hence I keep getting these annoying error messages all the time. Well, I know how to delete this file, but I believe that git should have deleted it when I called "git remote rm origin". Am I right? What is the purpose of this refs/remotes/origin/HEAD in the first place?

Anyway, thank you guys for this excellent tool and VCS/SCM system. Generally git is really joy to use and very powerful. Thanks.

#1 lore raw
Quoted 8 lines · Teemu Likonen

After a while I start tracking some other remote repository. I do this:

git remote add ng git://project-ng/project.git

Then I do "git fetch ng" and probably also "git checkout --track -b master-ng ng/master". Then I find the old "origin" useless to me so I remove it with "git remote rm origin".

Quoted 5 lines

It seems that refs/remotes/origin/HEAD is still left in my repository and hence I keep getting these annoying error messages all the time.

Well, I know how to delete this file, but I believe that git should have deleted it when I called "git remote rm origin". Am I right?

Good analysis. I would say "git remote rm" should have removed it and it would be a bug if it didn't.

Quoted 1 line

What is the purpose of this refs/remotes/origin/HEAD in the first place?

To let you say things like "git diff remotes/origin" as a short-hand for "git diff remotes/origin/master" (or whichever branch 'origin' repository considered the primary one, which is determined when you cloned from it).

#2 lore raw

Junio C Hamano kirjoitti:

Quoted 14 lines
Quoted 13 lines · Teemu Likonen

After a while I start tracking some other remote repository. I do this:

git remote add ng git://project-ng/project.git

Then I do "git fetch ng" and probably also "git checkout --track -b master-ng ng/master". Then I find the old "origin" useless to me so I remove it with "git remote rm origin".

It seems that refs/remotes/origin/HEAD is still left in my repository and hence I keep getting these annoying error messages all the time.

Quoted 5 lines
Quoted 2 lines

Well, I know how to delete this file, but I believe that git should have deleted it when I called "git remote rm origin". Am I right?

Good analysis. I would say "git remote rm" should have removed it and it would be a bug if it didn't.

Yes, "git remote rm <remote>" certainly doesn't remove the refs/remotes/<remote>/HEAD file. I can reproduce this bug anytime with current 'master' branch from git://git.kernel.org/pub/scm/git/git.git

#3 lore raw

PATCH 1/1 builtin remote rm: remove symbolic refs, too

In reply to Junio C Hamano

"git remote add" can add a symbolic ref "HEAD", and "rm" should delete it, too.

Noticed by Teemu Likonen.

---
 builtin-remote.c  |    5 +++++
 t/t5505-remote.sh |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
builtin-remote.c +5 -0
index aa90cc9..f7653b6 100644
@@ -267,6 +267,11 @@ static int add_branch_for_removal(const char *refname,
 
 	if (!prefixcmp(refname, branches->prefix)) {
 		struct path_list_item *item;
+
+		/* make sure that symrefs are deleted */
+		if (flags & REF_ISSYMREF)
+			return unlink(git_path(refname));
+
 		item = path_list_append(refname, branches->branches);
 		item->util = xmalloc(20);
 		hashcpy(item->util, sha1);
t/t5505-remote.sh +1 -0
index f45ea68..2822a65 100755
@@ -80,6 +80,7 @@ test_expect_success 'add another remote' '
 test_expect_success 'remove remote' '
 (
 	cd test &&
+	git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/master &&
 	git remote rm second
 )
 '
Signature · 1 line
1.5.4.3.653.gbc310
#4 lore raw

Re: [PATCH] builtin remote rm: remove symbolic refs, too

Quoted 16 lines · Johannes Schindelin

"git remote add" can add a symbolic ref "HEAD", and "rm" should delete it, too.

Noticed by Teemu Likonen.

---
 builtin-remote.c  |    5 +++++
 t/t5505-remote.sh |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
builtin-remote.c +0 -0
index aa90cc9..f7653b6 100644

builtin-remote.c static int add_branch_for_removal(const char *refname, · line 274

Quoted 8 lines
builtin-remote.c +5 -0
@@ -267,6 +267,11 @@ static int add_branch_for_removal(const char *refname,
 
 	if (!prefixcmp(refname, branches->prefix)) {
 		struct path_list_item *item;
+
+		/* make sure that symrefs are deleted */
+		if (flags & REF_ISSYMREF)
+			return unlink(git_path(refname));
+

Heh, doing this in C is much easier and simpler ;-)

#5 lore raw

[PATCH/RFC] Make "git remote rm <remote>" remove file refs/remotes/<remote>/HEAD

In reply to Teemu Likonen

The command "git remote rm <remote>" used to leave the refs/remotes/<remote>/HEAD file lying in the directory. This usually happens when user has cloned a remote repository and later decided to remove the default "origin" with "git remote rm origin". The result is that several git commans display the error message

error: refs/remotes/origin/HEAD points nowhere!

This patch makes "git remote rm" remove the HEAD file if it exists.

---

I have never written or even read any perl code before this (I'm not really a programmer) but I managed to come up with this. This seems to work well. If more error checking or something is needed, I guess somebody else has to do it; my skills aren't quite enough. :)

Any comments?

 git-remote.perl |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
git-remote.perl +6 -0
index b30ed73..f1f2a1a 100755
@@ -323,6 +323,7 @@ sub update_remote {
 
 sub rm_remote {
 	my ($name) = @_;
+	my $git_dir = $ENV{GIT_DIR} || ".git";
 	if (!exists $remote->{$name}) {
 		print STDERR "No such remote $name\n";
 		return 1;
@@ -340,6 +341,11 @@ sub rm_remote {
 		}
 	};
 
+	my $remotes_dir = "$git_dir/refs/remotes/$name";
+	if (-f "$remotes_dir/HEAD") {
+		unlink "$remotes_dir/HEAD";
+	}
+
 	my @refs = $git->command('for-each-ref',
 		'--format=%(refname) %(objectname)', "refs/remotes/$name");
 	for (@refs) {
Signature · 1 line
1.5.4.3.451.ga9908.dirty
#6 lore raw

Re: [PATCH/RFC] Make "git remote rm <remote>" remove file refs/remotes/<remote>/HEAD

Teemu Likonen kirjoitti:

Quoted 1 line

If more error checking or something is needed,

Such as finding the .git directory even if $PWD is somewhere else in the tree.

Quoted 2 lines

I guess
somebody else has to do it; my skills aren't quite enough. :)

I'm out.

#7 lore raw

Re: [PATCH/RFC] Make "git remote rm <remote>" remove file refs/remotes/<remote>/HEAD

In reply to Teemu Likonen
Quoted 6 lines · Junio C Hamano

I think "update-ref -d" should remove the ref without dereferencing anyway. How about not doing your patch at all (you would need to manually remove the symref in "prune" codepath as well), and instead doing this?

I do not know how this reacts when the tracked HEAD points at a ref that lexicographically sorts earlier, say branch "A".

And it turns out it does not react very well. You would want to do something like this on top of that one.

-- >8 --
"remote rm": first remove symbolic and then real refs

This teaches a new %(reftype) to for-each-ref that returns "symref", "packed", or "plain" (we never pack symrefs, so this is the complete enumeration), and updates "git remote rm" to first remove the symrefs and then the actual refs.

---

 builtin-for-each-ref.c |   13 +++++++++++++
 git-remote.perl        |   14 ++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
builtin-for-each-ref.c +13 -0
index 07d9c57..50e9eed 100644
@@ -31,6 +31,7 @@ struct ref_sort {
 
 struct refinfo {
 	char *refname;
+	int reftype;
 	unsigned char objectname[20];
 	struct atom_value *value;
 };
@@ -40,6 +41,7 @@ static struct {
 	cmp_type cmp_type;
 } valid_atom[] = {
 	{ "refname" },
+	{ "reftype" },
 	{ "objecttype" },
 	{ "objectsize", FIELD_ULONG },
 	{ "objectname" },
@@ -568,6 +570,16 @@ static void populate_value(struct refinfo *ref)
 			char *s = xmalloc(len + 4);
 			sprintf(s, "%s^{}", ref->refname);
 			v->s = s;
+		} else if (!strcmp(name, "reftype")) {
+			char *s = xmalloc(20);
+			/* We do not pack symrefs */
+			if (ref->reftype & REF_ISSYMREF)
+				strcpy(s, "symref");
+			else if (ref->reftype & REF_ISPACKED)
+				strcpy(s, "packed");
+			else
+				strcpy(s, "plain");
+			v->s = s;
 		}
 	}
 
@@ -658,6 +670,7 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f
 	 */
 	ref = xcalloc(1, sizeof(*ref));
 	ref->refname = xstrdup(refname);
+	ref->reftype = flag;
 	hashcpy(ref->objectname, sha1);
 
 	cnt = cb->grab_cnt;
git-remote.perl +12 -2
index b30ed73..c57e29d 100755
@@ -341,9 +341,19 @@ sub rm_remote {
 	};
 
 	my @refs = $git->command('for-each-ref',
-		'--format=%(refname) %(objectname)', "refs/remotes/$name");
+				 '--format=%(refname) %(reftype) %(objectname)',
+				 "refs/remotes/$name");
+	my @nonsym = ();
 	for (@refs) {
-		my ($ref, $object) = split;
+		my ($ref, $type, $object) = split;
+		if ($type !~ /symref/) {
+			push @nonsym, $_;
+		} else {
+			$git->command(qw(update-ref -d), $ref, $object);
+		}
+	}
+	for (@nonsym) {
+		my ($ref, $type, $object) = split;
 		$git->command(qw(update-ref -d), $ref, $object);
 	}
 	return 0;
#8 lore raw

Re: [PATCH/RFC] Make "git remote rm <remote>" remove file refs/remotes/<remote>/HEAD

Junio C Hamano kirjoitti:

Quoted 11 lines
Quoted 7 lines · Junio C Hamano

I think "update-ref -d" should remove the ref without dereferencing anyway. How about not doing your patch at all (you would need to manually remove the symref in "prune" codepath as well), and instead doing this?

I do not know how this reacts when the tracked HEAD points at a ref that lexicographically sorts earlier, say branch "A".

And it turns out it does not react very well. You would want to do something like this on top of that one.

This turned out to be much more complicated than just deleting a file. Thank you, these patches seem to work (at least from user's point of view).

#9 lore raw

Re: [PATCH/RFC] Make "git remote rm <remote>" remove file refs/remotes/<remote>/HEAD

In reply to Teemu Likonen
Quoted 11 lines · Teemu Likonen

The command "git remote rm <remote>" used to leave the refs/remotes/<remote>/HEAD file lying in the directory. This usually happens when user has cloned a remote repository and later decided to remove the default "origin" with "git remote rm origin". The result is that several git commans display the error message

error: refs/remotes/origin/HEAD points nowhere!

This patch makes "git remote rm" remove the HEAD file if it exists.

Quoted 8 lines

---

I have never written or even read any perl code before this (I'm not really a programmer) but I managed to come up with this. This seems to work well. If more error checking or something is needed, I guess somebody else has to do it; my skills aren't quite enough. :)

Any comments?

Hmm. The loop grabs everything under refs/remotes/$that_remote/ and runs "update-ref -d" on them.

I somehow had an impression that --no-deref option to update-ref was invented to allow operating on the symbolic ref itself, instead of operating on the ref it points at, but it does not seem to work.

I think "update-ref -d" should remove the ref without dereferencing anyway. How about not doing your patch at all (you would need to manually remove the symref in "prune" codepath as well), and instead doing this?

I do not know how this reacts when the tracked HEAD points at a ref that lexicographically sorts earlier, say branch "A".

-- >8 --
delete_ref(): when symref is given, delete it, not the underlying ref.

Removing the underlying ref without touching the symref does not make any sense. An alternative would be to remove the symref _and_ the underlying ref, but this should do for now.

---
 refs.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
refs.c +1 -1
index c979fb1..e7c0c38 100644
@@ -887,7 +887,7 @@ int delete_ref(const char *refname, const unsigned char *sha1)
 	struct ref_lock *lock;
 	int err, i, ret = 0, flag = 0;
 
-	lock = lock_ref_sha1_basic(refname, sha1, 0, &flag);
+	lock = lock_ref_sha1_basic(refname, sha1, REF_NODEREF, &flag);
 	if (!lock)
 		return 1;
 	if (!(flag & REF_ISPACKED)) {