[PATCH] Rename git-repo-config to git-config.
[git.git] / Documentation / git-checkout.txt
blobc44a4a8004a49e42c01bcd238ae22b8d32b33cc3
1 git-checkout(1)
2 ===============
4 NAME
5 ----
6 git-checkout - Checkout and switch to a branch
8 SYNOPSIS
9 --------
10 [verse]
11 'git-checkout' [-f] [-b <new_branch> [-l]] [-m] [<branch>]
12 'git-checkout' [<branch>] <paths>...
14 DESCRIPTION
15 -----------
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.
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.
32 OPTIONS
33 -------
34 -f::
35         Force a re-read of everything.
37 -b::
38         Create a new branch named <new_branch> and start it at
39         <branch>.  The new branch name must pass all checks defined
40         by gitlink:git-check-ref-format[1].  Some of these checks
41         may restrict the characters allowed in a branch name.
43 -l::
44         Create the new branch's ref log.  This activates recording of
45         all changes to made the branch ref, enabling use of date
46         based sha1 expressions such as "<branchname>@{yesterday}".
48 -m::
49         If you have local modifications to one or more files that
50         are different between the current branch and the branch to
51         which you are switching, the command refuses to switch
52         branches in order to preserve your modifications in context.
53         However, with this option, a three-way merge between the current
54         branch, your working tree contents, and the new branch
55         is done, and you will be on the new branch.
57 When a merge conflict happens, the index entries for conflicting
58 paths are left unmerged, and you need to resolve the conflicts
59 and mark the resolved paths with `git update-index`.
61 <new_branch>::
62         Name for the new branch.
64 <branch>::
65         Branch to checkout; may be any object ID that resolves to a
66         commit.  Defaults to HEAD.
68 When this parameter names a non-branch (but still a valid commit object),
69 your HEAD becomes 'detached'.
72 Detached HEAD
73 -------------
75 It is sometimes useful to be able to 'checkout' a commit that is
76 not at the tip of one of your branches.  The most obvious
77 example is to check out the commit at a tagged official release
78 point, like this:
80 ------------
81 $ git checkout v2.6.18
82 ------------
84 Earlier versions of git did not allow this and asked you to
85 create a temporary branch using `-b` option, but starting from
86 version 1.5.0, the above command 'detaches' your HEAD from the
87 current branch and directly point at the commit named by the tag
88 (`v2.6.18` in the above example).
90 You can use usual git commands while in this state.  You can use
91 `git-reset --hard $othercommit` to further move around, for
92 example.  You can make changes and create a new commit on top of
93 a detached HEAD.  You can even create a merge by using `git
94 merge $othercommit`.
96 The state you are in while your HEAD is detached is not recorded
97 by any branch (which is natural --- you are not on any branch).
98 What this means is that you can discard your temporary commits
99 and merges by switching back to an existing branch (e.g. `git
100 checkout master`), and a later `git prune` or `git gc` would
101 garbage-collect them.
103 The command would refuse to switch back to make sure that you do
104 not discard your temporary state by mistake when your detached
105 HEAD is not pointed at by any existing ref.  If you did want to
106 save your state (e.g. "I was interested in the fifth commit from
107 the top of 'master' branch", or "I made two commits to fix minor
108 bugs while on a detached HEAD" -- and if you do not want to lose
109 these facts), you can create a new branch and switch to it with
110 `git checkout -b newbranch` so that you can keep building on
111 that state, or tag it first so that you can come back to it
112 later and switch to the branch you wanted to switch to with `git
113 tag that_state; git checkout master`.  On the other hand, if you
114 did want to discard the temporary state, you can give `-f`
115 option (e.g. `git checkout -f master`) to override this
116 behaviour.
119 EXAMPLES
120 --------
122 . The following sequence checks out the `master` branch, reverts
123 the `Makefile` to two revisions back, deletes hello.c by
124 mistake, and gets it back from the index.
126 ------------
127 $ git checkout master             <1>
128 $ git checkout master~2 Makefile  <2>
129 $ rm -f hello.c
130 $ git checkout hello.c            <3>
131 ------------
133 <1> switch branch
134 <2> take out a file out of other commit
135 <3> restore hello.c from HEAD of current branch
137 If you have an unfortunate branch that is named `hello.c`, this
138 step would be confused as an instruction to switch to that branch.
139 You should instead write:
141 ------------
142 $ git checkout -- hello.c
143 ------------
145 . After working in a wrong branch, switching to the correct
146 branch would be done using:
148 ------------
149 $ git checkout mytopic
150 ------------
152 However, your "wrong" branch and correct "mytopic" branch may
153 differ in files that you have locally modified, in which case,
154 the above checkout would fail like this:
156 ------------
157 $ git checkout mytopic
158 fatal: Entry 'frotz' not uptodate. Cannot merge.
159 ------------
161 You can give the `-m` flag to the command, which would try a
162 three-way merge:
164 ------------
165 $ git checkout -m mytopic
166 Auto-merging frotz
167 ------------
169 After this three-way merge, the local modifications are _not_
170 registered in your index file, so `git diff` would show you what
171 changes you made since the tip of the new branch.
173 . When a merge conflict happens during switching branches with
174 the `-m` option, you would see something like this:
176 ------------
177 $ git checkout -m mytopic
178 Auto-merging frotz
179 merge: warning: conflicts during merge
180 ERROR: Merge conflict in frotz
181 fatal: merge program failed
182 ------------
184 At this point, `git diff` shows the changes cleanly merged as in
185 the previous example, as well as the changes in the conflicted
186 files.  Edit and resolve the conflict and mark it resolved with
187 `git update-index` as usual:
189 ------------
190 $ edit frotz
191 $ git update-index frotz
192 ------------
195 Author
196 ------
197 Written by Linus Torvalds <torvalds@osdl.org>
199 Documentation
200 --------------
201 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
205 Part of the gitlink:git[7] suite