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 | |
64 | and mark the resolved paths with `git update-index`. | |
65 | ||
0270f7c5 LAS |
66 | <new_branch>:: |
67 | Name for the new branch. | |
7fc9d69f | 68 | |
0270f7c5 LAS |
69 | <branch>:: |
70 | Branch to checkout; may be any object ID that resolves to a | |
5e1a2e8c JH |
71 | commit. Defaults to HEAD. |
72 | + | |
73 | When this parameter names a non-branch (but still a valid commit object), | |
74 | your HEAD becomes 'detached'. | |
75 | ||
76 | ||
77 | Detached HEAD | |
78 | ------------- | |
79 | ||
80 | It is sometimes useful to be able to 'checkout' a commit that is | |
81 | not at the tip of one of your branches. The most obvious | |
82 | example is to check out the commit at a tagged official release | |
83 | point, like this: | |
84 | ||
85 | ------------ | |
86 | $ git checkout v2.6.18 | |
87 | ------------ | |
88 | ||
89 | Earlier versions of git did not allow this and asked you to | |
90 | create a temporary branch using `-b` option, but starting from | |
91 | version 1.5.0, the above command 'detaches' your HEAD from the | |
92 | current branch and directly point at the commit named by the tag | |
93 | (`v2.6.18` in the above example). | |
94 | ||
95 | You can use usual git commands while in this state. You can use | |
96 | `git-reset --hard $othercommit` to further move around, for | |
97 | example. You can make changes and create a new commit on top of | |
98 | a detached HEAD. You can even create a merge by using `git | |
99 | merge $othercommit`. | |
100 | ||
101 | The state you are in while your HEAD is detached is not recorded | |
102 | by any branch (which is natural --- you are not on any branch). | |
103 | What this means is that you can discard your temporary commits | |
104 | and merges by switching back to an existing branch (e.g. `git | |
105 | checkout master`), and a later `git prune` or `git gc` would | |
106 | garbage-collect them. | |
107 | ||
108 | The command would refuse to switch back to make sure that you do | |
109 | not discard your temporary state by mistake when your detached | |
110 | HEAD is not pointed at by any existing ref. If you did want to | |
111 | save your state (e.g. "I was interested in the fifth commit from | |
112 | the top of 'master' branch", or "I made two commits to fix minor | |
113 | bugs while on a detached HEAD" -- and if you do not want to lose | |
114 | these facts), you can create a new branch and switch to it with | |
115 | `git checkout -b newbranch` so that you can keep building on | |
116 | that state, or tag it first so that you can come back to it | |
117 | later and switch to the branch you wanted to switch to with `git | |
118 | tag that_state; git checkout master`. On the other hand, if you | |
119 | did want to discard the temporary state, you can give `-f` | |
120 | option (e.g. `git checkout -f master`) to override this | |
121 | behaviour. | |
7fc9d69f | 122 | |
4aaa7027 | 123 | |
1be0659e JH |
124 | EXAMPLES |
125 | -------- | |
4aaa7027 | 126 | |
1be0659e | 127 | . The following sequence checks out the `master` branch, reverts |
4aaa7027 JH |
128 | the `Makefile` to two revisions back, deletes hello.c by |
129 | mistake, and gets it back from the index. | |
1be0659e | 130 | + |
4aaa7027 | 131 | ------------ |
48aeecdc SE |
132 | $ git checkout master <1> |
133 | $ git checkout master~2 Makefile <2> | |
4aaa7027 | 134 | $ rm -f hello.c |
48aeecdc SE |
135 | $ git checkout hello.c <3> |
136 | ------------ | |
137 | + | |
1e2ccd3a JH |
138 | <1> switch branch |
139 | <2> take out a file out of other commit | |
48aeecdc | 140 | <3> restore hello.c from HEAD of current branch |
1be0659e | 141 | + |
48aeecdc SE |
142 | If you have an unfortunate branch that is named `hello.c`, this |
143 | step would be confused as an instruction to switch to that branch. | |
144 | You should instead write: | |
1be0659e | 145 | + |
4aaa7027 JH |
146 | ------------ |
147 | $ git checkout -- hello.c | |
148 | ------------ | |
149 | ||
1be0659e | 150 | . After working in a wrong branch, switching to the correct |
71bb1033 | 151 | branch would be done using: |
1be0659e JH |
152 | + |
153 | ------------ | |
154 | $ git checkout mytopic | |
155 | ------------ | |
156 | + | |
157 | However, your "wrong" branch and correct "mytopic" branch may | |
158 | differ in files that you have locally modified, in which case, | |
159 | the above checkout would fail like this: | |
160 | + | |
161 | ------------ | |
162 | $ git checkout mytopic | |
163 | fatal: Entry 'frotz' not uptodate. Cannot merge. | |
164 | ------------ | |
165 | + | |
166 | You can give the `-m` flag to the command, which would try a | |
167 | three-way merge: | |
168 | + | |
169 | ------------ | |
170 | $ git checkout -m mytopic | |
171 | Auto-merging frotz | |
172 | ------------ | |
173 | + | |
174 | After this three-way merge, the local modifications are _not_ | |
175 | registered in your index file, so `git diff` would show you what | |
176 | changes you made since the tip of the new branch. | |
177 | ||
178 | . When a merge conflict happens during switching branches with | |
179 | the `-m` option, you would see something like this: | |
180 | + | |
181 | ------------ | |
182 | $ git checkout -m mytopic | |
183 | Auto-merging frotz | |
184 | merge: warning: conflicts during merge | |
185 | ERROR: Merge conflict in frotz | |
186 | fatal: merge program failed | |
187 | ------------ | |
188 | + | |
189 | At this point, `git diff` shows the changes cleanly merged as in | |
190 | the previous example, as well as the changes in the conflicted | |
191 | files. Edit and resolve the conflict and mark it resolved with | |
192 | `git update-index` as usual: | |
193 | + | |
194 | ------------ | |
195 | $ edit frotz | |
196 | $ git update-index frotz | |
197 | ------------ | |
198 | ||
4aaa7027 | 199 | |
7fc9d69f JH |
200 | Author |
201 | ------ | |
202 | Written by Linus Torvalds <torvalds@osdl.org> | |
203 | ||
204 | Documentation | |
205 | -------------- | |
206 | Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>. | |
207 | ||
208 | GIT | |
209 | --- | |
a7154e91 | 210 | Part of the gitlink:git[7] suite |
7fc9d69f | 211 |