@matheusbsbs / matheus-bagliano / commits / 4aaa702794
Fork 0

git-checkout: revert specific paths to either index or a given tree-ish.

When extra paths arguments are given, git-checkout reverts only those paths to either the version recorded in the index or the version recorded in the given tree-ish. This has been on the TODO list for quite a while. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 18, 2005 at 01:29 UTC 4aaa702794447d9b281dd22fe532fd61e02434e1
2 files changed +103 -18
Documentation/git-checkout.txt
+39 -3
@@ -7,12 +7,24 @@ git-checkout - Checkout and switch to a branch.
7
8 SYNOPSIS
9 --------
10 -'git-checkout' [-f] [-b <new_branch>] [<branch>]
10 +'git-checkout' [-f] [-b <new_branch>] [<branch>] [<paths>...]
11
12 DESCRIPTION
13 -----------
14 -Updates the index and working tree to reflect the specified branch,
15 -<branch>. Updates HEAD to be <branch> or, if specified, <new_branch>.
14 +
15 +When <paths> are not given, this command switches branches, by
16 +updating the index and working tree to reflect the specified
17 +branch, <branch>, and updating HEAD to be <branch> or, if
18 +specified, <new_branch>.
19 +
20 +When <paths> are given, this command does *not* switch
21 +branches. It updates the named paths in the working tree from
22 +the index file (i.e. it runs `git-checkout-index -f -u`). In
23 +this case, `-f` and `-b` options are meaningless and giving
24 +either of them results in an error. <branch> argument can be
25 +used to specify a specific tree-ish to update the index for the
26 +given paths before updating the working tree.
27 +
28
29 OPTIONS
30 -------
@@ -29,6 +41,30 @@ OPTIONS
41 Branch to checkout; may be any object ID that resolves to a
42 commit. Defaults to HEAD.
43
44 +
45 +EXAMPLE
46 +-------
47 +
48 +The following sequence checks out the `master` branch, reverts
49 +the `Makefile` to two revisions back, deletes hello.c by
50 +mistake, and gets it back from the index.
51 +
52 +------------
53 +$ git checkout master
54 +$ git checkout master~2 Makefile
55 +$ rm -f hello.c
56 +$ git checkout hello.c
57 +------------
58 +
59 +If you have an unfortunate branch that is named `hello.c`, the
60 +last step above would be confused as an instruction to switch to
61 +that branch. You should instead write:
62 +
63 +------------
64 +$ git checkout -- hello.c
65 +------------
66 +
67 +
68 Author
69 ------
70 Written by Linus Torvalds <torvalds@osdl.org>
git-checkout.sh
+64 -15
@@ -23,32 +23,81 @@ while [ "$#" != "0" ]; do
23 "-f")
24 force=1
25 ;;
26 + --)
27 + break
28 + ;;
29 *)
27 - rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) ||
28 - die "I don't know any '$arg'."
29 - if [ -z "$rev" ]; then
30 - echo "unknown flag $arg"
31 - exit 1
32 - fi
33 - if [ "$new" ]; then
34 - echo "Multiple revisions?"
35 - exit 1
36 - fi
37 - new="$rev"
38 - if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
39 - branch="$arg"
30 + if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
31 + then
32 + if [ -z "$rev" ]; then
33 + echo "unknown flag $arg"
34 + exit 1
35 + fi
36 + new="$rev"
37 + if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
38 + branch="$arg"
39 + fi
40 + elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
41 + then
42 + # checking out selected paths from a tree-ish.
43 + new="$rev"
44 + branch=
45 + else
46 + new=
47 + branch=
48 + set x "$arg" "$@"
49 + shift
50 fi
51 + break
52 ;;
53 esac
54 done
44 -[ -z "$new" ] && new=$old
55
56 +# The behaviour of the command with and without explicit path
57 +# parameters is quite different.
58 +#
59 +# Without paths, we are checking out everything in the work tree,
60 +# possibly switching branches. This is the traditional behaviour.
61 #
62 +# With paths, we are _never_ switching branch, but checking out
63 +# the named paths from either index (when no rev is given),
64 +# or the named tree-ish (when rev is given).
65 +
66 +if test "$#" -ge 1
67 +then
68 + if test '' != "$newbranch$force"
69 + then
70 + die "updating paths and switching branches or forcing are incompatible."
71 + fi
72 + if test '' != "$new"
73 + then
74 + # from a specific tree-ish; note that this is for
75 + # rescuing paths and is never meant to remove what
76 + # is not in the named tree-ish.
77 + git-ls-tree -r "$new" "$@" |
78 + sed -ne 's/^\([0-7]*\) blob \(.*\)$/\1 \2/p' |
79 + git-update-index --index-info || exit $?
80 + fi
81 + git-checkout-index -f -u -- "$@"
82 + exit $?
83 +else
84 + # Make sure we did not fall back on $arg^{tree} codepath
85 + # since we are not checking out from an arbitrary tree-ish,
86 + # but switching branches.
87 + if test '' != "$new"
88 + then
89 + git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
90 + die "Cannot switch branch to a non-commit."
91 + fi
92 +fi
93 +
94 +[ -z "$new" ] && new=$old
95 +
96 # If we don't have an old branch that we're switching to,
97 # and we don't have a new branch name for the target we
98 # are switching to, then we'd better just be checking out
99 # what we already had
51 -#
100 +
101 [ -z "$branch$newbranch" ] &&
102 [ "$new" != "$old" ] &&
103 die "git checkout: you need to specify a new branch name"