6 git-checkout - Checkout and switch to a branch
10 'git-checkout' [-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]
15 When <paths> are not given, this command switches branches, by
16 updating the index and working tree to reflect the specified
17 branch, <branch>, and updating HEAD to be <branch> or, if
18 specified, <new_branch>.
20 When <paths> are given, this command does *not* switch
21 branches. It updates the named paths in the working tree from
22 the index file (i.e. it runs `git-checkout-index -f -u`). In
23 this case, `-f` and `-b` options are meaningless and giving
24 either of them results in an error. <branch> argument can be
25 used to specify a specific tree-ish to update the index for the
26 given paths before updating the working tree.
32 Force an re-read of everything.
35 Create a new branch and start it at <branch>.
38 If you have local modifications to a file that is
39 different between the current branch and the branch you
40 are switching to, the command refuses to switch
41 branches, to preserve your modifications in context.
42 With this option, a three-way merge between the current
43 branch, your working tree contents, and the new branch
44 is done, and you will be on the new branch.
46 When a merge conflict happens, the index entries for conflicting
47 paths are left unmerged, and you need to resolve the conflicts
48 and mark the resolved paths with `git update-index`.
51 Name for the new branch.
54 Branch to checkout; may be any object ID that resolves to a
55 commit. Defaults to HEAD.
61 . The following sequence checks out the `master` branch, reverts
62 the `Makefile` to two revisions back, deletes hello.c by
63 mistake, and gets it back from the index.
66 $ git checkout master <1>
67 $ git checkout master~2 Makefile <2>
69 $ git checkout hello.c <3>
72 <2> take out a file out of other commit
73 <3> or "git checkout -- hello.c", as in the next example.
76 If you have an unfortunate branch that is named `hello.c`, the
77 last step above would be confused as an instruction to switch to
78 that branch. You should instead write:
81 $ git checkout -- hello.c
84 . After working in a wrong branch, switching to the correct
85 branch you would want to is done with:
88 $ git checkout mytopic
91 However, your "wrong" branch and correct "mytopic" branch may
92 differ in files that you have locally modified, in which case,
93 the above checkout would fail like this:
96 $ git checkout mytopic
97 fatal: Entry 'frotz' not uptodate. Cannot merge.
100 You can give the `-m` flag to the command, which would try a
104 $ git checkout -m mytopic
108 After this three-way merge, the local modifications are _not_
109 registered in your index file, so `git diff` would show you what
110 changes you made since the tip of the new branch.
112 . When a merge conflict happens during switching branches with
113 the `-m` option, you would see something like this:
116 $ git checkout -m mytopic
118 merge: warning: conflicts during merge
119 ERROR: Merge conflict in frotz
120 fatal: merge program failed
123 At this point, `git diff` shows the changes cleanly merged as in
124 the previous example, as well as the changes in the conflicted
125 files. Edit and resolve the conflict and mark it resolved with
126 `git update-index` as usual:
130 $ git update-index frotz
136 Written by Linus Torvalds <torvalds@osdl.org>
140 Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
144 Part of the gitlink:git[7] suite