Documentation: detached HEAD
[git/git.git] / Documentation / git-checkout.txt
CommitLineData
215a7ad1
JH
1git-checkout(1)
2===============
7fc9d69f
JH
3
4NAME
5----
7bd7f280 6git-checkout - Checkout and switch to a branch
7fc9d69f
JH
7
8SYNOPSIS
9--------
71bb1033 10[verse]
969d326d 11'git-checkout' [-f] [-b <new_branch> [-l]] [-m] [<branch>]
5e1a2e8c 12'git-checkout' [<branch>] <paths>...
7fc9d69f
JH
13
14DESCRIPTION
15-----------
4aaa7027 16
71bb1033 17When <paths> are not given, this command switches branches by
4aaa7027
JH
18updating the index and working tree to reflect the specified
19branch, <branch>, and updating HEAD to be <branch> or, if
71bb1033
JL
20specified, <new_branch>. Using -b will cause <new_branch> to
21be created.
4aaa7027
JH
22
23When <paths> are given, this command does *not* switch
24branches. It updates the named paths in the working tree from
25the index file (i.e. it runs `git-checkout-index -f -u`). In
26this case, `-f` and `-b` options are meaningless and giving
27either of them results in an error. <branch> argument can be
28used to specify a specific tree-ish to update the index for the
29given paths before updating the working tree.
30
7fc9d69f
JH
31
32OPTIONS
33-------
0270f7c5 34-f::
71bb1033 35 Force a re-read of everything.
0270f7c5
LAS
36
37-b::
2b1f4247
SP
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.
7fc9d69f 42
969d326d
SP
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}".
47
1be0659e 48-m::
71bb1033
JL
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
1be0659e
JH
54 branch, your working tree contents, and the new branch
55 is done, and you will be on the new branch.
56+
57When a merge conflict happens, the index entries for conflicting
58paths are left unmerged, and you need to resolve the conflicts
59and mark the resolved paths with `git update-index`.
60
0270f7c5
LAS
61<new_branch>::
62 Name for the new branch.
7fc9d69f 63
0270f7c5
LAS
64<branch>::
65 Branch to checkout; may be any object ID that resolves to a
5e1a2e8c
JH
66 commit. Defaults to HEAD.
67+
68When this parameter names a non-branch (but still a valid commit object),
69your HEAD becomes 'detached'.
70
71
72Detached HEAD
73-------------
74
75It is sometimes useful to be able to 'checkout' a commit that is
76not at the tip of one of your branches. The most obvious
77example is to check out the commit at a tagged official release
78point, like this:
79
80------------
81$ git checkout v2.6.18
82------------
83
84Earlier versions of git did not allow this and asked you to
85create a temporary branch using `-b` option, but starting from
86version 1.5.0, the above command 'detaches' your HEAD from the
87current branch and directly point at the commit named by the tag
88(`v2.6.18` in the above example).
89
90You can use usual git commands while in this state. You can use
91`git-reset --hard $othercommit` to further move around, for
92example. You can make changes and create a new commit on top of
93a detached HEAD. You can even create a merge by using `git
94merge $othercommit`.
95
96The state you are in while your HEAD is detached is not recorded
97by any branch (which is natural --- you are not on any branch).
98What this means is that you can discard your temporary commits
99and merges by switching back to an existing branch (e.g. `git
100checkout master`), and a later `git prune` or `git gc` would
101garbage-collect them.
102
103The command would refuse to switch back to make sure that you do
104not discard your temporary state by mistake when your detached
105HEAD is not pointed at by any existing ref. If you did want to
106save your state (e.g. "I was interested in the fifth commit from
107the top of 'master' branch", or "I made two commits to fix minor
108bugs while on a detached HEAD" -- and if you do not want to lose
109these facts), you can create a new branch and switch to it with
110`git checkout -b newbranch` so that you can keep building on
111that state, or tag it first so that you can come back to it
112later and switch to the branch you wanted to switch to with `git
113tag that_state; git checkout master`. On the other hand, if you
114did want to discard the temporary state, you can give `-f`
115option (e.g. `git checkout -f master`) to override this
116behaviour.
7fc9d69f 117
4aaa7027 118
1be0659e
JH
119EXAMPLES
120--------
4aaa7027 121
1be0659e 122. The following sequence checks out the `master` branch, reverts
4aaa7027
JH
123the `Makefile` to two revisions back, deletes hello.c by
124mistake, and gets it back from the index.
1be0659e 125+
4aaa7027 126------------
48aeecdc
SE
127$ git checkout master <1>
128$ git checkout master~2 Makefile <2>
4aaa7027 129$ rm -f hello.c
48aeecdc
SE
130$ git checkout hello.c <3>
131------------
132+
1e2ccd3a
JH
133<1> switch branch
134<2> take out a file out of other commit
48aeecdc 135<3> restore hello.c from HEAD of current branch
1be0659e 136+
48aeecdc
SE
137If you have an unfortunate branch that is named `hello.c`, this
138step would be confused as an instruction to switch to that branch.
139You should instead write:
1be0659e 140+
4aaa7027
JH
141------------
142$ git checkout -- hello.c
143------------
144
1be0659e 145. After working in a wrong branch, switching to the correct
71bb1033 146branch would be done using:
1be0659e
JH
147+
148------------
149$ git checkout mytopic
150------------
151+
152However, your "wrong" branch and correct "mytopic" branch may
153differ in files that you have locally modified, in which case,
154the above checkout would fail like this:
155+
156------------
157$ git checkout mytopic
158fatal: Entry 'frotz' not uptodate. Cannot merge.
159------------
160+
161You can give the `-m` flag to the command, which would try a
162three-way merge:
163+
164------------
165$ git checkout -m mytopic
166Auto-merging frotz
167------------
168+
169After this three-way merge, the local modifications are _not_
170registered in your index file, so `git diff` would show you what
171changes you made since the tip of the new branch.
172
173. When a merge conflict happens during switching branches with
174the `-m` option, you would see something like this:
175+
176------------
177$ git checkout -m mytopic
178Auto-merging frotz
179merge: warning: conflicts during merge
180ERROR: Merge conflict in frotz
181fatal: merge program failed
182------------
183+
184At this point, `git diff` shows the changes cleanly merged as in
185the previous example, as well as the changes in the conflicted
186files. Edit and resolve the conflict and mark it resolved with
187`git update-index` as usual:
188+
189------------
190$ edit frotz
191$ git update-index frotz
192------------
193
4aaa7027 194
7fc9d69f
JH
195Author
196------
197Written by Linus Torvalds <torvalds@osdl.org>
198
199Documentation
200--------------
201Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
202
203GIT
204---
a7154e91 205Part of the gitlink:git[7] suite
7fc9d69f 206