Documentation: Describe merge operation a bit better.
[git/jnareb-git.git] / git-checkout.sh
blob4cf30e2c0529e90886e6edfd485cec67a278e224
1 #!/bin/sh
2 . git-sh-setup
4 usage () {
5 die "usage: git checkout [-f] [-b <new_branch>] [<branch>] [<paths>...]"
8 old=$(git-rev-parse HEAD)
9 new=
10 force=
11 branch=
12 newbranch=
13 while [ "$#" != "0" ]; do
14 arg="$1"
15 shift
16 case "$arg" in
17 "-b")
18 newbranch="$1"
19 shift
20 [ -z "$newbranch" ] &&
21 die "git checkout: -b needs a branch name"
22 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
23 die "git checkout: branch $newbranch already exists"
24 git-check-ref-format "heads/$newbranch" ||
25 die "we do not like '$newbranch' as a branch name."
27 "-f")
28 force=1
30 --)
31 break
33 -*)
34 usage
37 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
38 then
39 if [ -z "$rev" ]; then
40 echo "unknown flag $arg"
41 exit 1
43 new="$rev"
44 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
45 branch="$arg"
47 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
48 then
49 # checking out selected paths from a tree-ish.
50 new="$rev"
51 branch=
52 else
53 new=
54 branch=
55 set x "$arg" "$@"
56 shift
58 break
60 esac
61 done
63 # The behaviour of the command with and without explicit path
64 # parameters is quite different.
66 # Without paths, we are checking out everything in the work tree,
67 # possibly switching branches. This is the traditional behaviour.
69 # With paths, we are _never_ switching branch, but checking out
70 # the named paths from either index (when no rev is given),
71 # or the named tree-ish (when rev is given).
73 if test "$#" -ge 1
74 then
75 if test '' != "$newbranch$force"
76 then
77 die "updating paths and switching branches or forcing are incompatible."
79 if test '' != "$new"
80 then
81 # from a specific tree-ish; note that this is for
82 # rescuing paths and is never meant to remove what
83 # is not in the named tree-ish.
84 git-ls-tree -r "$new" "$@" |
85 git-update-index --index-info || exit $?
87 git-checkout-index -f -u -- "$@"
88 exit $?
89 else
90 # Make sure we did not fall back on $arg^{tree} codepath
91 # since we are not checking out from an arbitrary tree-ish,
92 # but switching branches.
93 if test '' != "$new"
94 then
95 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
96 die "Cannot switch branch to a non-commit."
100 [ -z "$new" ] && new=$old
102 # If we don't have an old branch that we're switching to,
103 # and we don't have a new branch name for the target we
104 # are switching to, then we'd better just be checking out
105 # what we already had
107 [ -z "$branch$newbranch" ] &&
108 [ "$new" != "$old" ] &&
109 die "git checkout: you need to specify a new branch name"
111 if [ "$force" ]
112 then
113 git-read-tree --reset $new &&
114 git-checkout-index -q -f -u -a
115 else
116 git-update-index --refresh >/dev/null
117 git-read-tree -m -u $old $new
121 # Switch the HEAD pointer to the new branch if it we
122 # checked out a branch head, and remove any potential
123 # old MERGE_HEAD's (subsequent commits will clearly not
124 # be based on them, since we re-set the index)
126 if [ "$?" -eq 0 ]; then
127 if [ "$newbranch" ]; then
128 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
129 mkdir -p "$GIT_DIR/$leading" &&
130 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
131 branch="$newbranch"
133 [ "$branch" ] &&
134 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
135 rm -f "$GIT_DIR/MERGE_HEAD"
136 else
137 exit 1