1#!/bin/sh 2. git-sh-setup|| die "Not a git archive" 3 4old=$(git-rev-parse HEAD) 5new= 6force= 7branch= 8newbranch= 9while["$#"!="0"];do 10 arg="$1" 11shift 12case"$arg"in 13"-b") 14 newbranch="$1" 15shift 16[-z"$newbranch"] && 17 die "git checkout: -b needs a branch name" 18[-e"$GIT_DIR/refs/heads/$newbranch"] && 19 die "git checkout: branch$newbranchalready exists" 20;; 21"-f") 22 force=1 23;; 24*) 25rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)|| 26 die "I don't know any '$arg'." 27if[-z"$rev"];then 28echo"unknown flag$arg" 29exit1 30fi 31if["$new"];then 32echo"Multiple revisions?" 33exit1 34fi 35 new="$rev" 36if[-f"$GIT_DIR/refs/heads/$arg"];then 37 branch="$arg" 38fi 39;; 40esac 41done 42[-z"$new"] && new=$old 43 44# 45# If we don't have an old branch that we're switching to, 46# and we don't have a new branch name for the target we 47# are switching to, then we'd better just be checking out 48# what we already had 49# 50[-z"$branch$newbranch"] && 51["$new"!="$old"] && 52 die "git checkout: you need to specify a new branch name" 53 54if["$force"] 55then 56 git-read-tree --reset$new&& 57 git-checkout-index -q -f -u -a 58else 59 git-update-index --refresh>/dev/null 60 git-read-tree -m -u$old $new 61fi 62 63# 64# Switch the HEAD pointer to the new branch if it we 65# checked out a branch head, and remove any potential 66# old MERGE_HEAD's (subsequent commits will clearly not 67# be based on them, since we re-set the index) 68# 69if["$?"-eq0];then 70if["$newbranch"];then 71echo$new>"$GIT_DIR/refs/heads/$newbranch" 72 branch="$newbranch" 73fi 74["$branch"] && 75 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch" 76rm-f"$GIT_DIR/MERGE_HEAD" 77else 78exit1 79fi