1GIT-CHECKOUT(1)                   Git Manual                   GIT-CHECKOUT(1)
2
3
4

NAME

6       git-checkout - Checkout and switch to a branch
7

SYNOPSIS

9           git-checkout [-q] [-f] [[--track | --no-track] -b <new_branch> [-l]] [-m] [<branch>]
10           git-checkout [<tree-ish>] <paths>...
11

DESCRIPTION

13       When <paths> are not given, this command switches branches by updating
14       the index and working tree to reflect the specified branch, <branch>,
15       and updating HEAD to be <branch> or, if specified, <new_branch>. Using
16       -b will cause <new_branch> to be created; in this case you can use the
17       --track or --no-track options, which will be passed to git branch.
18
19       When <paths> are given, this command does not switch branches. It
20       updates the named paths in the working tree from the index file (i.e.
21       it runs git-checkout-index -f -u), or from a named commit. In this
22       case, the -f and -b options are meaningless and giving either of them
23       results in an error. <tree-ish> argument can be used to specify a
24       specific tree-ish (i.e. commit, tag or tree) to update the index for
25       the given paths before updating the working tree.
26

OPTIONS

28       -q
29           Quiet, suppress feedback messages.
30
31       -f
32           Proceed even if the index or the working tree differs from HEAD.
33           This is used to throw away local changes.
34
35       -b
36           Create a new branch named <new_branch> and start it at <branch>.
37           The new branch name must pass all checks defined by git-check-ref-
38           format(1). Some of these checks may restrict the characters allowed
39           in a branch name.
40
41       --track
42           When -b is given and a branch is created off a remote branch, set
43           up configuration so that git-pull will automatically retrieve data
44           from the remote branch. Set the branch.autosetupmerge configuration
45           variable to true if you want git-checkout and git-branch to always
46           behave as if --track were given.
47
48       --no-track
49           When -b is given and a branch is created off a remote branch, set
50           up configuration so that git-pull will not retrieve data from the
51           remote branch, ignoring the branch.autosetupmerge configuration
52           variable.
53
54       -l
55           Create the new branch´s reflog. This activates recording of all
56           changes made to the branch ref, enabling use of date based sha1
57           expressions such as "<branchname>@{yesterday}".
58
59       -m
60           If you have local modifications to one or more files that are
61           different between the current branch and the branch to which you
62           are switching, the command refuses to switch branches in order to
63           preserve your modifications in context. However, with this option,
64           a three-way merge between the current branch, your working tree
65           contents, and the new branch is done, and you will be on the new
66           branch.
67
68           When a merge conflict happens, the index entries for conflicting
69           paths are left unmerged, and you need to resolve the conflicts and
70           mark the resolved paths with git add (or git rm if the merge should
71           result in deletion of the path).
72
73       <new_branch>
74           Name for the new branch.
75
76       <branch>
77           Branch to checkout; may be any object ID that resolves to a commit.
78           Defaults to HEAD.
79
80           When this parameter names a non-branch (but still a valid commit
81           object), your HEAD becomes detached.
82

DETACHED HEAD

84       It is sometimes useful to be able to checkout a commit that is not at
85       the tip of one of your branches. The most obvious example is to check
86       out the commit at a tagged official release point, like this:
87
88
89
90           $ git checkout v2.6.18
91
92       Earlier versions of git did not allow this and asked you to create a
93       temporary branch using -b option, but starting from version 1.5.0, the
94       above command detaches your HEAD from the current branch and directly
95       point at the commit named by the tag (v2.6.18 in the above example).
96
97       You can use usual git commands while in this state. You can use
98       git-reset --hard $othercommit to further move around, for example. You
99       can make changes and create a new commit on top of a detached HEAD. You
100       can even create a merge by using git merge $othercommit.
101
102       The state you are in while your HEAD is detached is not recorded by any
103       branch (which is natural --- you are not on any branch). What this
104       means is that you can discard your temporary commits and merges by
105       switching back to an existing branch (e.g. git checkout master), and a
106       later git prune or git gc would garbage-collect them. If you did this
107       by mistake, you can ask the reflog for HEAD where you were, e.g.
108
109
110
111           $ git log -g -2 HEAD
112
113

EXAMPLES

115        1.  The following sequence checks out the master branch, reverts the
116           Makefile to two revisions back, deletes hello.c by mistake, and
117           gets it back from the index.
118
119
120
121               $ git checkout master             (1)
122               $ git checkout master~2 Makefile  (2)
123               $ rm -f hello.c
124               $ git checkout hello.c            (3)
125
126
127           1. switch branch
128           2. take out a file out of other commit
129           3. restore hello.c from HEAD of current branch
130
131           If you have an unfortunate branch that is named hello.c, this step
132           would be confused as an instruction to switch to that branch. You
133           should instead write:
134
135
136
137               $ git checkout -- hello.c
138
139
140        2.  After working in a wrong branch, switching to the correct branch
141           would be done using:
142
143
144
145               $ git checkout mytopic
146
147           However, your "wrong" branch and correct "mytopic" branch may
148           differ in files that you have locally modified, in which case, the
149           above checkout would fail like this:
150
151
152
153               $ git checkout mytopic
154               fatal: Entry ´frotz´ not uptodate. Cannot merge.
155
156           You can give the -m flag to the command, which would try a
157           three-way merge:
158
159
160
161               $ git checkout -m mytopic
162               Auto-merging frotz
163
164           After this three-way merge, the local modifications are not
165           registered in your index file, so git diff would show you what
166           changes you made since the tip of the new branch.
167
168        3.  When a merge conflict happens during switching branches with the
169           -m option, you would see something like this:
170
171
172
173               $ git checkout -m mytopic
174               Auto-merging frotz
175               merge: warning: conflicts during merge
176               ERROR: Merge conflict in frotz
177               fatal: merge program failed
178
179           At this point, git diff shows the changes cleanly merged as in the
180           previous example, as well as the changes in the conflicted files.
181           Edit and resolve the conflict and mark it resolved with git add as
182           usual:
183
184
185
186               $ edit frotz
187               $ git add frotz
188
189

AUTHOR

191       Written by Linus Torvalds <torvalds@osdl.org>
192

DOCUMENTATION

194       Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
195

GIT

197       Part of the git(7) suite
198
199
200
201
202Git 1.5.3.3                       10/09/2007                   GIT-CHECKOUT(1)
Impressum