From 91dcdfd3b5331d955cfb60edf8930f1b5c142905 Mon Sep 17 00:00:00 2001
From: Linus Torvalds <torvalds@g5.osdl.org>
Date: Mon, 11 Jul 2005 20:44:20 -0700
Subject: [PATCH] Make "git checkout" create new branches on demand

In particular, if we check out something that isn't an old branch, it
now requires a new branch-name to check the thing out into.

So, for example:

	git checkout -b my-branch v2.6.12

will create the new branch "my-branch", and start it at v2.6.12, while

	git checkout master

will just switch back to the master branch.

Of course, if you want to create a new branch "my-branch" and _not_
check it out, you could have done so with just

	git-rev-parse v2.6.12^0 > .git/refs/heads/my-branch

which I think I will codify as "git branch".
---
 git-checkout-script | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/git-checkout-script b/git-checkout-script
index 48e1da9399b..7e70338c476 100755
--- a/git-checkout-script
+++ b/git-checkout-script
@@ -5,10 +5,19 @@ old=$(git-rev-parse HEAD)
 new=
 force=
 branch=
+newbranch=
 while [ "$#" != "0" ]; do
     arg="$1"
     shift
     case "$arg" in
+	"-b")
+		newbranch="$1"
+		shift
+		[ -z "$newbranch" ] &&
+			die "git checkout: -b needs a branch name"
+		[ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
+			die "git checkout: branch $newbranch already exists"
+		;;
 	"-f")
 		force=1
 		;;
@@ -32,6 +41,16 @@ while [ "$#" != "0" ]; do
 done
 [ -z "$new" ] && new=$old
 
+#
+# If we don't have an old branch that we're switching to,
+# and we don't have a new branch name for the target we
+# are switching to, then we'd better just be checking out
+# what we already had
+#
+[ -z "$branch$newbranch" ] &&
+	[ "$new" != "$old" ] &&
+	die "git checkout: you need to specify a new branch name"
+
 if [ "$force" ]
 then
     git-read-tree --reset $new &&
@@ -47,6 +66,10 @@ fi
 # be based on them, since we re-set the index)
 #
 if [ "$?" -eq 0 ]; then
+	if [ "$newbranch" ]; then
+		echo $new > "$GIT_DIR/refs/heads/$newbranch"
+		branch="$newbranch"
+	fi
 	[ "$branch" ] && ln -sf "refs/heads/$branch" "$GIT_DIR/HEAD"
 	rm -f "$GIT_DIR/MERGE_HEAD"
 fi