6 git-checkout - Checkout and switch to a branch
11 'git-checkout' [-f] [-b <new_branch> [-l]] [-m] [<branch>]
12 'git-checkout' [-m] [<branch>] <paths>...
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
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.
35 Force a re-read of everything.
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.
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}".
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`.
62 Name for the new branch.
65 Branch to checkout; may be any object ID that resolves to a
66 commit. Defaults to HEAD.
72 . The following sequence checks out the `master` branch, reverts
73 the `Makefile` to two revisions back, deletes hello.c by
74 mistake, and gets it back from the index.
77 $ git checkout master <1>
78 $ git checkout master~2 Makefile <2>
80 $ git checkout hello.c <3>
84 <2> take out a file out of other commit
85 <3> restore hello.c from HEAD of current branch
87 If you have an unfortunate branch that is named `hello.c`, this
88 step would be confused as an instruction to switch to that branch.
89 You should instead write:
92 $ git checkout -- hello.c
95 . After working in a wrong branch, switching to the correct
96 branch would be done using:
99 $ git checkout mytopic
102 However, your "wrong" branch and correct "mytopic" branch may
103 differ in files that you have locally modified, in which case,
104 the above checkout would fail like this:
107 $ git checkout mytopic
108 fatal: Entry 'frotz' not uptodate. Cannot merge.
111 You can give the `-m` flag to the command, which would try a
115 $ git checkout -m mytopic
119 After this three-way merge, the local modifications are _not_
120 registered in your index file, so `git diff` would show you what
121 changes you made since the tip of the new branch.
123 . When a merge conflict happens during switching branches with
124 the `-m` option, you would see something like this:
127 $ git checkout -m mytopic
129 merge: warning: conflicts during merge
130 ERROR: Merge conflict in frotz
131 fatal: merge program failed
134 At this point, `git diff` shows the changes cleanly merged as in
135 the previous example, as well as the changes in the conflicted
136 files. Edit and resolve the conflict and mark it resolved with
137 `git update-index` as usual:
141 $ git update-index frotz
147 Written by Linus Torvalds <torvalds@osdl.org>
151 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
155 Part of the gitlink:git[7] suite