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' [-q] [-f] [[--track | --no-track] -b <new_branch> [-l]] [-m] [<branch>]
12 'git-checkout' [<tree-ish>] <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; in this case you can use the --track or --no-track
22 options, which will be passed to `git branch`.
24 When <paths> are given, this command does *not* switch
25 branches. It updates the named paths in the working tree from
26 the index file (i.e. it runs `git-checkout-index -f -u`), or
27 from a named commit. In
28 this case, the `-f` and `-b` options are meaningless and giving
29 either of them results in an error. <tree-ish> argument can be
30 used to specify a specific tree-ish (i.e. commit, tag or tree)
31 to update the index for the given paths before updating the
32 working tree.
35 OPTIONS
36 -------
37 -q::
38 Quiet, supress feedback messages.
40 -f::
41 Proceed even if the index or the working tree differs
42 from HEAD. This is used to throw away local changes.
44 -b::
45 Create a new branch named <new_branch> and start it at
46 <branch>. The new branch name must pass all checks defined
47 by gitlink:git-check-ref-format[1]. Some of these checks
48 may restrict the characters allowed in a branch name.
50 --track::
51 When -b is given and a branch is created off a remote branch,
52 set up configuration so that git-pull will automatically
53 retrieve data from the remote branch. Set the
54 branch.autosetupmerge configuration variable to true if you
55 want git-checkout and git-branch to always behave as if
56 '--track' were given.
58 --no-track::
59 When -b is given and a branch is created off a remote branch,
60 set up configuration so that git-pull will not retrieve data
61 from the remote branch, ignoring the branch.autosetupmerge
62 configuration variable.
64 -l::
65 Create the new branch's ref log. This activates recording of
66 all changes to made the branch ref, enabling use of date
67 based sha1 expressions such as "<branchname>@{yesterday}".
69 -m::
70 If you have local modifications to one or more files that
71 are different between the current branch and the branch to
72 which you are switching, the command refuses to switch
73 branches in order to preserve your modifications in context.
74 However, with this option, a three-way merge between the current
75 branch, your working tree contents, and the new branch
76 is done, and you will be on the new branch.
77 +
78 When a merge conflict happens, the index entries for conflicting
79 paths are left unmerged, and you need to resolve the conflicts
80 and mark the resolved paths with `git add` (or `git rm` if the merge
81 should result in deletion of the path).
83 <new_branch>::
84 Name for the new branch.
86 <branch>::
87 Branch to checkout; may be any object ID that resolves to a
88 commit. Defaults to HEAD.
89 +
90 When this parameter names a non-branch (but still a valid commit object),
91 your HEAD becomes 'detached'.
94 Detached HEAD
95 -------------
97 It is sometimes useful to be able to 'checkout' a commit that is
98 not at the tip of one of your branches. The most obvious
99 example is to check out the commit at a tagged official release
100 point, like this:
102 ------------
103 $ git checkout v2.6.18
104 ------------
106 Earlier versions of git did not allow this and asked you to
107 create a temporary branch using `-b` option, but starting from
108 version 1.5.0, the above command 'detaches' your HEAD from the
109 current branch and directly point at the commit named by the tag
110 (`v2.6.18` in the above example).
112 You can use usual git commands while in this state. You can use
113 `git-reset --hard $othercommit` to further move around, for
114 example. You can make changes and create a new commit on top of
115 a detached HEAD. You can even create a merge by using `git
116 merge $othercommit`.
118 The state you are in while your HEAD is detached is not recorded
119 by any branch (which is natural --- you are not on any branch).
120 What this means is that you can discard your temporary commits
121 and merges by switching back to an existing branch (e.g. `git
122 checkout master`), and a later `git prune` or `git gc` would
123 garbage-collect them. If you did this by mistake, you can ask
124 the reflog for HEAD where you were, e.g.
126 ------------
127 $ git log -g -2 HEAD
128 ------------
131 EXAMPLES
132 --------
134 . The following sequence checks out the `master` branch, reverts
135 the `Makefile` to two revisions back, deletes hello.c by
136 mistake, and gets it back from the index.
137 +
138 ------------
139 $ git checkout master <1>
140 $ git checkout master~2 Makefile <2>
141 $ rm -f hello.c
142 $ git checkout hello.c <3>
143 ------------
144 +
145 <1> switch branch
146 <2> take out a file out of other commit
147 <3> restore hello.c from HEAD of current branch
148 +
149 If you have an unfortunate branch that is named `hello.c`, this
150 step would be confused as an instruction to switch to that branch.
151 You should instead write:
152 +
153 ------------
154 $ git checkout -- hello.c
155 ------------
157 . After working in a wrong branch, switching to the correct
158 branch would be done using:
159 +
160 ------------
161 $ git checkout mytopic
162 ------------
163 +
164 However, your "wrong" branch and correct "mytopic" branch may
165 differ in files that you have locally modified, in which case,
166 the above checkout would fail like this:
167 +
168 ------------
169 $ git checkout mytopic
170 fatal: Entry 'frotz' not uptodate. Cannot merge.
171 ------------
172 +
173 You can give the `-m` flag to the command, which would try a
174 three-way merge:
175 +
176 ------------
177 $ git checkout -m mytopic
178 Auto-merging frotz
179 ------------
180 +
181 After this three-way merge, the local modifications are _not_
182 registered in your index file, so `git diff` would show you what
183 changes you made since the tip of the new branch.
185 . When a merge conflict happens during switching branches with
186 the `-m` option, you would see something like this:
187 +
188 ------------
189 $ git checkout -m mytopic
190 Auto-merging frotz
191 merge: warning: conflicts during merge
192 ERROR: Merge conflict in frotz
193 fatal: merge program failed
194 ------------
195 +
196 At this point, `git diff` shows the changes cleanly merged as in
197 the previous example, as well as the changes in the conflicted
198 files. Edit and resolve the conflict and mark it resolved with
199 `git add` as usual:
200 +
201 ------------
202 $ edit frotz
203 $ git add frotz
204 ------------
207 Author
208 ------
209 Written by Linus Torvalds <torvalds@osdl.org>
211 Documentation
212 --------------
213 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
215 GIT
216 ---
217 Part of the gitlink:git[7] suite