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