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

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

From
TLTeemu Likonen <tlikonen@iki.fi>
Date
Mar 7, 2008, 21:02 UTC
Message-ID
<1204923732-29141-1-git-send-email-tlikonen@iki.fi>
In-Reply-To
<200803051338.44938.tlikonen@iki.fi>

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.
Signed-off-by: Teemu Likonen <tlikonen@iki.fi>
---

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(-)
diff --git a/git-remote.perl b/git-remote.perl
index b30ed73..f1f2a1a 100755
--- a/git-remote.perl
+++ b/git-remote.perl
@@ -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) {
-- 
1.5.4.3.451.ga9908.dirty
Previous: Junio C HamanoNext: Teemu Likonen
Message 6 of 10 in “error: refs/remotes/origin/HEAD points nowhere!”
  1. Teemu LikonenMar 5, 2008
  2. Junio C HamanoMar 5, 2008
  3. Teemu LikonenMar 6, 2008
  4. builtin remote rm: remove symbolic refs, tooJohannes Schindelin, Mar 8, 2008
  5. Junio C HamanoMar 9, 2008
  6. Make "git remote rm <remote>" remove file refs/remotes/<remote>/HEADTeemu Likonen, Mar 7, 2008
  7. Teemu LikonenMar 7, 2008
  8. Junio C HamanoMar 8, 2008
  9. Junio C HamanoMar 8, 2008
  10. Teemu LikonenMar 8, 2008

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.