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

Teach "git checkout" to use git-show-ref

From
Linus Torvalds <torvalds@osdl.org>
Date
Sep 15, 2006, 21:56 UTC
Message-ID
<Pine.LNX.4.64.0609151455150.4388@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.64.0609151108560.4388@g5.osdl.org>
That way, it doesn't care how the refs are stored any more
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

Just as an example of how to use git-show-ref rather than knowing about "$GIT_DIR/refs/..." paths.

This basically replaces my much hackier version that is in the "lt/refs" branch, but can go into the master branch independently of the refs-packing work.

diff --git a/git-checkout.sh b/git-checkout.sh
index 580a9e8..6e4c535 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -22,7 +22,7 @@ while [ "$#" != "0" ]; do
 		shift
 		[ -z "$newbranch" ] &&
 			die "git checkout: -b needs a branch name"
-		[ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
+		git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
 			die "git checkout: branch $newbranch already exists"
 		git-check-ref-format "heads/$newbranch" ||
 			die "git checkout: we do not like '$newbranch' as a branch name."
@@ -51,7 +51,7 @@ while [ "$#" != "0" ]; do
 			fi
 			new="$rev"
 			new_name="$arg^0"
-			if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
+			if git-show-ref --verify --quiet -- "refs/heads/$arg" ]; then
 				branch="$arg"
 			fi
 		elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
Previous: Junio C HamanoNext: Jakub Narebski
Message 5 of 7 in “Add "git show-ref" builtin command”
  1. Linus TorvaldsSep 15, 2006
  2. Junio C HamanoSep 15, 2006
  3. Jakub NarebskiSep 15, 2006
  4. Junio C HamanoSep 15, 2006
  5. Teach "git checkout" to use git-show-refLinus Torvalds, Sep 15, 2006
  6. Jakub NarebskiSep 15, 2006
  7. Junio C HamanoSep 16, 2006

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.