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