1git-checkout(1) 2=============== 3 4NAME 5---- 6git-checkout - Checkout a branch or paths to the working tree 7 8SYNOPSIS 9-------- 10[verse] 11'git checkout' [-q] [-f] [-m] [-b <new_branch>] [<branch>] 12'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>... 13 14DESCRIPTION 15----------- 16 17When <paths> are not given, this command switches branches by 18updating the index and working tree to reflect the specified 19branch, <branch>, and updating HEAD to be <branch> or, if 20specified, <new_branch>. Using -b will cause <new_branch> to 21be created as if linkgit:git-branch[1] were called; in this case you can 22use the --track or --no-track options, which will be passed to `git 23branch`. 24 25As a convenience, --track will default to creating a branch whose 26name is constructed from the specified branch name by stripping 27the first namespace level. 28 29When <paths> are given, this command does *not* switch 30branches. It updates the named paths in the working tree from 31the index file, or from a named <tree-ish> (most often a commit). In 32this case, the `-b` and `--track` options are meaningless and giving 33either of them results in an error. The <tree-ish> argument can be 34used to specify a specific tree-ish (i.e. commit, tag or tree) 35to update the index for the given paths before updating the 36working tree. 37 38The index may contain unmerged entries after a failed merge. By 39default, if you try to check out such an entry from the index, the 40checkout operation will fail and nothing will be checked out. 41Using -f will ignore these unmerged entries. The contents from a 42specific side of the merge can be checked out of the index by 43using --ours or --theirs. With -m, changes made to the working tree 44file can be discarded to recreate the original conflicted merge result. 45 46OPTIONS 47------- 48-q:: 49 Quiet, suppress feedback messages. 50 51-f:: 52 When switching branches, proceed even if the index or the 53 working tree differs from HEAD. This is used to throw away 54 local changes. 55+ 56When checking out paths from the index, do not fail upon unmerged 57entries; instead, unmerged entries are ignored. 58 59--ours:: 60--theirs:: 61 When checking out paths from the index, check out stage #2 62 ('ours') or #3 ('theirs') for unmerged paths. 63 64-b:: 65 Create a new branch named <new_branch> and start it at 66 <branch>; see linkgit:git-branch[1] for details. 67 68-t:: 69--track:: 70 When creating a new branch, set up "upstream" configuration. See 71 "--track" in linkgit:git-branch[1] for details. 72+ 73If no '-b' option is given, the name of the new branch will be 74derived from the remote branch. If "remotes/" or "refs/remotes/" 75is prefixed it is stripped away, and then the part up to the 76next slash (which would be the nickname of the remote) is removed. 77This would tell us to use "hack" as the local branch when branching 78off of "origin/hack" (or "remotes/origin/hack", or even 79"refs/remotes/origin/hack"). If the given name has no slash, or the above 80guessing results in an empty name, the guessing is aborted. You can 81explicitly give a name with '-b' in such a case. 82 83--no-track:: 84 Do not set up "upstream" configuration, even if the 85 branch.autosetupmerge configuration variable is true. 86 87-l:: 88 Create the new branch's reflog; see linkgit:git-branch[1] for 89 details. 90 91-m:: 92--merge:: 93 When switching branches, 94 if you have local modifications to one or more files that 95 are different between the current branch and the branch to 96 which you are switching, the command refuses to switch 97 branches in order to preserve your modifications in context. 98 However, with this option, a three-way merge between the current 99 branch, your working tree contents, and the new branch 100 is done, and you will be on the new branch. 101+ 102When a merge conflict happens, the index entries for conflicting 103paths are left unmerged, and you need to resolve the conflicts 104and mark the resolved paths with `git add` (or `git rm` if the merge 105should result in deletion of the path). 106+ 107When checking out paths from the index, this option lets you recreate 108the conflicted merge in the specified paths. 109 110--conflict=<style>:: 111 The same as --merge option above, but changes the way the 112 conflicting hunks are presented, overriding the 113 merge.conflictstyle configuration variable. Possible values are 114 "merge" (default) and "diff3" (in addition to what is shown by 115 "merge" style, shows the original contents). 116 117<new_branch>:: 118 Name for the new branch. 119 120<tree-ish>:: 121 Tree to checkout from (when paths are given). If not specified, 122 the index will be used. 123 124<branch>:: 125 Branch to checkout (when no paths are given); may be any object 126 ID that resolves to a commit. Defaults to HEAD. 127+ 128When this parameter names a non-branch (but still a valid commit object), 129your HEAD becomes 'detached'. 130+ 131As a special case, the `"@\{-N\}"` syntax for the N-th last branch 132checks out the branch (instead of detaching). You may also specify 133`-` which is synonymous with `"@\{-1\}"`. 134 135 136Detached HEAD 137------------- 138 139It is sometimes useful to be able to 'checkout' a commit that is 140not at the tip of one of your branches. The most obvious 141example is to check out the commit at a tagged official release 142point, like this: 143 144------------ 145$ git checkout v2.6.18 146------------ 147 148Earlier versions of git did not allow this and asked you to 149create a temporary branch using the `-b` option, but starting from 150version 1.5.0, the above command 'detaches' your HEAD from the 151current branch and directly points at the commit named by the tag 152(`v2.6.18` in the example above). 153 154You can use all git commands while in this state. You can use 155`git reset --hard $othercommit` to further move around, for 156example. You can make changes and create a new commit on top of 157a detached HEAD. You can even create a merge by using `git 158merge $othercommit`. 159 160The state you are in while your HEAD is detached is not recorded 161by any branch (which is natural --- you are not on any branch). 162What this means is that you can discard your temporary commits 163and merges by switching back to an existing branch (e.g. `git 164checkout master`), and a later `git prune` or `git gc` would 165garbage-collect them. If you did this by mistake, you can ask 166the reflog for HEAD where you were, e.g. 167 168------------ 169$ git log -g -2 HEAD 170------------ 171 172 173EXAMPLES 174-------- 175 176. The following sequence checks out the `master` branch, reverts 177the `Makefile` to two revisions back, deletes hello.c by 178mistake, and gets it back from the index. 179+ 180------------ 181$ git checkout master <1> 182$ git checkout master~2 Makefile <2> 183$ rm -f hello.c 184$ git checkout hello.c <3> 185------------ 186+ 187<1> switch branch 188<2> take a file out of another commit 189<3> restore hello.c from the index 190+ 191If you have an unfortunate branch that is named `hello.c`, this 192step would be confused as an instruction to switch to that branch. 193You should instead write: 194+ 195------------ 196$ git checkout -- hello.c 197------------ 198 199. After working in the wrong branch, switching to the correct 200branch would be done using: 201+ 202------------ 203$ git checkout mytopic 204------------ 205+ 206However, your "wrong" branch and correct "mytopic" branch may 207differ in files that you have modified locally, in which case 208the above checkout would fail like this: 209+ 210------------ 211$ git checkout mytopic 212fatal: Entry 'frotz' not uptodate. Cannot merge. 213------------ 214+ 215You can give the `-m` flag to the command, which would try a 216three-way merge: 217+ 218------------ 219$ git checkout -m mytopic 220Auto-merging frotz 221------------ 222+ 223After this three-way merge, the local modifications are _not_ 224registered in your index file, so `git diff` would show you what 225changes you made since the tip of the new branch. 226 227. When a merge conflict happens during switching branches with 228the `-m` option, you would see something like this: 229+ 230------------ 231$ git checkout -m mytopic 232Auto-merging frotz 233ERROR: Merge conflict in frotz 234fatal: merge program failed 235------------ 236+ 237At this point, `git diff` shows the changes cleanly merged as in 238the previous example, as well as the changes in the conflicted 239files. Edit and resolve the conflict and mark it resolved with 240`git add` as usual: 241+ 242------------ 243$ edit frotz 244$ git add frotz 245------------ 246 247 248Author 249------ 250Written by Linus Torvalds <torvalds@osdl.org> 251 252Documentation 253-------------- 254Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>. 255 256GIT 257--- 258Part of the linkgit:git[1] suite