Enable ref log creation in git checkout -b.
[git.git] / Documentation / git-checkout.txt
1 git-checkout(1)
2 ===============
3
4 NAME
5 ----
6 git-checkout - Checkout and switch to a branch
7
8 SYNOPSIS
9 --------
10 [verse]
11 'git-checkout' [-f] [-b <new_branch> [-l]] [-m] [<branch>]
12 'git-checkout' [-m] [<branch>] <paths>...
13
14 DESCRIPTION
15 -----------
16
17 When <paths> are not given, this command switches branches by
18 updating the index and working tree to reflect the specified
19 branch, <branch>, and updating HEAD to be <branch> or, if
20 specified, <new_branch>.  Using -b will cause <new_branch> to
21 be created.
22
23 When <paths> are given, this command does *not* switch
24 branches.  It updates the named paths in the working tree from
25 the index file (i.e. it runs `git-checkout-index -f -u`).  In
26 this case, `-f` and `-b` options are meaningless and giving
27 either of them results in an error.  <branch> argument can be
28 used to specify a specific tree-ish to update the index for the
29 given paths before updating the working tree.
30
31
32 OPTIONS
33 -------
34 -f::
35         Force a re-read of everything.
36
37 -b::
38         Create a new branch and start it at <branch>.
39
40 -l::
41         Create the new branch's ref log.  This activates recording of
42         all changes to made the branch ref, enabling use of date
43         based sha1 expressions such as "<branchname>@{yesterday}".
44
45 -m::
46         If you have local modifications to one or more files that
47         are different between the current branch and the branch to
48         which you are switching, the command refuses to switch
49         branches in order to preserve your modifications in context.
50         However, with this option, a three-way merge between the current
51         branch, your working tree contents, and the new branch
52         is done, and you will be on the new branch.
53 +
54 When a merge conflict happens, the index entries for conflicting
55 paths are left unmerged, and you need to resolve the conflicts
56 and mark the resolved paths with `git update-index`.
57
58 <new_branch>::
59         Name for the new branch.
60
61 <branch>::
62         Branch to checkout; may be any object ID that resolves to a
63         commit. Defaults to HEAD.
64
65
66 EXAMPLES
67 --------
68
69 . The following sequence checks out the `master` branch, reverts
70 the `Makefile` to two revisions back, deletes hello.c by
71 mistake, and gets it back from the index.
72 +
73 ------------
74 $ git checkout master             <1>
75 $ git checkout master~2 Makefile  <2>
76 $ rm -f hello.c
77 $ git checkout hello.c            <3>
78 ------------
79 +
80 <1> switch branch
81 <2> take out a file out of other commit
82 <3> restore hello.c from HEAD of current branch
83 +
84 If you have an unfortunate branch that is named `hello.c`, this
85 step would be confused as an instruction to switch to that branch.
86 You should instead write:
87 +
88 ------------
89 $ git checkout -- hello.c
90 ------------
91
92 . After working in a wrong branch, switching to the correct
93 branch would be done using:
94 +
95 ------------
96 $ git checkout mytopic
97 ------------
98 +
99 However, your "wrong" branch and correct "mytopic" branch may
100 differ in files that you have locally modified, in which case,
101 the above checkout would fail like this:
102 +
103 ------------
104 $ git checkout mytopic
105 fatal: Entry 'frotz' not uptodate. Cannot merge.
106 ------------
107 +
108 You can give the `-m` flag to the command, which would try a
109 three-way merge:
110 +
111 ------------
112 $ git checkout -m mytopic
113 Auto-merging frotz
114 ------------
115 +
116 After this three-way merge, the local modifications are _not_
117 registered in your index file, so `git diff` would show you what
118 changes you made since the tip of the new branch.
119
120 . When a merge conflict happens during switching branches with
121 the `-m` option, you would see something like this:
122 +
123 ------------
124 $ git checkout -m mytopic
125 Auto-merging frotz
126 merge: warning: conflicts during merge
127 ERROR: Merge conflict in frotz
128 fatal: merge program failed
129 ------------
130 +
131 At this point, `git diff` shows the changes cleanly merged as in
132 the previous example, as well as the changes in the conflicted
133 files.  Edit and resolve the conflict and mark it resolved with
134 `git update-index` as usual:
135 +
136 ------------
137 $ edit frotz
138 $ git update-index frotz
139 ------------
140
141
142 Author
143 ------
144 Written by Linus Torvalds <torvalds@osdl.org>
145
146 Documentation
147 --------------
148 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
149
150 GIT
151 ---
152 Part of the gitlink:git[7] suite
153