]>
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] |
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 | |
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 | 20 | specified, <new_branch>. Using -b will cause <new_branch> to |
0746d19a PB |
21 | be created; in this case you can use the --track or --no-track |
22 | options, which will be passed to `git branch`. | |
4aaa7027 JH |
23 | |
24 | When <paths> are given, this command does *not* switch | |
25 | branches. It updates the named paths in the working tree from | |
40c8279f BF |
26 | the index file (i.e. it runs `git-checkout-index -f -u`), or |
27 | from a named commit. In | |
28 | this case, the `-f` and `-b` options are meaningless and giving | |
84a978f1 JH |
29 | either of them results in an error. <tree-ish> argument can be |
30 | used to specify a specific tree-ish (i.e. commit, tag or tree) | |
31 | to update the index for the given paths before updating the | |
32 | working tree. | |
4aaa7027 | 33 | |
7fc9d69f JH |
34 | |
35 | OPTIONS | |
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 | + | |
78 | When a merge conflict happens, the index entries for conflicting | |
79 | paths are left unmerged, and you need to resolve the conflicts | |
d7f078b8 SP |
80 | and mark the resolved paths with `git add` (or `git rm` if the merge |
81 | should 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 | + | |
90 | When this parameter names a non-branch (but still a valid commit object), | |
91 | your HEAD becomes 'detached'. | |
92 | ||
93 | ||
94 | Detached HEAD | |
95 | ------------- | |
96 | ||
97 | It is sometimes useful to be able to 'checkout' a commit that is | |
98 | not at the tip of one of your branches. The most obvious | |
99 | example is to check out the commit at a tagged official release | |
100 | point, like this: | |
101 | ||
102 | ------------ | |
103 | $ git checkout v2.6.18 | |
104 | ------------ | |
105 | ||
106 | Earlier versions of git did not allow this and asked you to | |
107 | create a temporary branch using `-b` option, but starting from | |
108 | version 1.5.0, the above command 'detaches' your HEAD from the | |
109 | current branch and directly point at the commit named by the tag | |
110 | (`v2.6.18` in the above example). | |
111 | ||
112 | You can use usual git commands while in this state. You can use | |
113 | `git-reset --hard $othercommit` to further move around, for | |
114 | example. You can make changes and create a new commit on top of | |
115 | a detached HEAD. You can even create a merge by using `git | |
116 | merge $othercommit`. | |
117 | ||
118 | The state you are in while your HEAD is detached is not recorded | |
119 | by any branch (which is natural --- you are not on any branch). | |
120 | What this means is that you can discard your temporary commits | |
121 | and merges by switching back to an existing branch (e.g. `git | |
122 | checkout master`), and a later `git prune` or `git gc` would | |
cec8d146 JH |
123 | garbage-collect them. If you did this by mistake, you can ask |
124 | the reflog for HEAD where you were, e.g. | |
125 | ||
126 | ------------ | |
127 | $ git log -g -2 HEAD | |
128 | ------------ | |
7fc9d69f | 129 | |
4aaa7027 | 130 | |
1be0659e JH |
131 | EXAMPLES |
132 | -------- | |
4aaa7027 | 133 | |
1be0659e | 134 | . The following sequence checks out the `master` branch, reverts |
4aaa7027 JH |
135 | the `Makefile` to two revisions back, deletes hello.c by |
136 | mistake, 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 |
149 | If you have an unfortunate branch that is named `hello.c`, this |
150 | step would be confused as an instruction to switch to that branch. | |
151 | You 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 | 158 | branch would be done using: |
1be0659e JH |
159 | + |
160 | ------------ | |
161 | $ git checkout mytopic | |
162 | ------------ | |
163 | + | |
164 | However, your "wrong" branch and correct "mytopic" branch may | |
165 | differ in files that you have locally modified, in which case, | |
166 | the above checkout would fail like this: | |
167 | + | |
168 | ------------ | |
169 | $ git checkout mytopic | |
170 | fatal: Entry 'frotz' not uptodate. Cannot merge. | |
171 | ------------ | |
172 | + | |
173 | You can give the `-m` flag to the command, which would try a | |
174 | three-way merge: | |
175 | + | |
176 | ------------ | |
177 | $ git checkout -m mytopic | |
178 | Auto-merging frotz | |
179 | ------------ | |
180 | + | |
181 | After this three-way merge, the local modifications are _not_ | |
182 | registered in your index file, so `git diff` would show you what | |
183 | changes you made since the tip of the new branch. | |
184 | ||
185 | . When a merge conflict happens during switching branches with | |
186 | the `-m` option, you would see something like this: | |
187 | + | |
188 | ------------ | |
189 | $ git checkout -m mytopic | |
190 | Auto-merging frotz | |
191 | merge: warning: conflicts during merge | |
192 | ERROR: Merge conflict in frotz | |
193 | fatal: merge program failed | |
194 | ------------ | |
195 | + | |
196 | At this point, `git diff` shows the changes cleanly merged as in | |
197 | the previous example, as well as the changes in the conflicted | |
198 | files. 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 |
207 | Author |
208 | ------ | |
209 | Written by Linus Torvalds <torvalds@osdl.org> | |
210 | ||
211 | Documentation | |
212 | -------------- | |
213 | Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>. | |
214 | ||
215 | GIT | |
216 | --- | |
a7154e91 | 217 | Part of the gitlink:git[7] suite |