Documentation / git-checkout.txton commit Documentation: alter checkout --orphan description (feb98d1)
   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] [<branch>]
  12'git checkout' [-q] [-f] [-m] [[-b|--orphan] <new_branch>] [<start_point>]
  13'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
  14'git checkout' --patch [<tree-ish>] [--] [<paths>...]
  15
  16DESCRIPTION
  17-----------
  18
  19When <paths> are not given, this command switches branches by
  20updating the index, working tree, and HEAD to reflect the specified
  21branch.
  22
  23If `-b` is given, a new branch is created and checked out, as if
  24linkgit:git-branch[1] were called; in this case you can
  25use the --track or --no-track options, which will be passed to `git
  26branch`.  As a convenience, --track without `-b` implies branch
  27creation; see the description of --track below.
  28
  29When <paths> or --patch 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::
  50        Quiet, suppress feedback messages.
  51
  52-f::
  53--force::
  54        When switching branches, proceed even if the index or the
  55        working tree differs from HEAD.  This is used to throw away
  56        local changes.
  57+
  58When checking out paths from the index, do not fail upon unmerged
  59entries; instead, unmerged entries are ignored.
  60
  61--ours::
  62--theirs::
  63        When checking out paths from the index, check out stage #2
  64        ('ours') or #3 ('theirs') for unmerged paths.
  65
  66-b::
  67        Create a new branch named <new_branch> and start it at
  68        <start_point>; see linkgit:git-branch[1] for details.
  69
  70-t::
  71--track::
  72        When creating a new branch, set up "upstream" configuration. See
  73        "--track" in linkgit:git-branch[1] for details.
  74+
  75If no '-b' option is given, the name of the new branch will be
  76derived from the remote branch.  If "remotes/" or "refs/remotes/"
  77is prefixed it is stripped away, and then the part up to the
  78next slash (which would be the nickname of the remote) is removed.
  79This would tell us to use "hack" as the local branch when branching
  80off of "origin/hack" (or "remotes/origin/hack", or even
  81"refs/remotes/origin/hack").  If the given name has no slash, or the above
  82guessing results in an empty name, the guessing is aborted.  You can
  83explicitly give a name with '-b' in such a case.
  84
  85--no-track::
  86        Do not set up "upstream" configuration, even if the
  87        branch.autosetupmerge configuration variable is true.
  88
  89-l::
  90        Create the new branch's reflog; see linkgit:git-branch[1] for
  91        details.
  92
  93--orphan::
  94        Create a new 'orphan' branch, named <new_branch>, started from
  95        <start_point> and switch to it.  The first commit made on this
  96        new branch will have no parents and it will be the root of a new
  97        history totally disconnected from all the other branches and
  98        commits.
  99+
 100The index and the working tree are adjusted as if you had previously run
 101"git checkout <start_point>".  This allows you to start a new history
 102that records a set of paths similar to <start_point> by easily running
 103"git commit -a" to make the root commit.
 104+
 105This can be useful when you want to publish the tree from a commit
 106without exposing its full history. You might want to do this to publish
 107an open source branch of a project whose current tree is "clean", but
 108whose full history contains proprietary or otherwise encumbered bits of
 109code.
 110+
 111If you want to start a disconnected history that records a set of paths
 112that is totally different from the one of <start_point>, then you should
 113clear the index and the working tree right after creating the orphan
 114branch by running "git rm -rf ." from the top level of the working tree.
 115Afterwards you will be ready to prepare your new files, repopulating the
 116working tree, by copying them from elsewhere, extracting a tarball, etc.
 117
 118-m::
 119--merge::
 120        When switching branches,
 121        if you have local modifications to one or more files that
 122        are different between the current branch and the branch to
 123        which you are switching, the command refuses to switch
 124        branches in order to preserve your modifications in context.
 125        However, with this option, a three-way merge between the current
 126        branch, your working tree contents, and the new branch
 127        is done, and you will be on the new branch.
 128+
 129When a merge conflict happens, the index entries for conflicting
 130paths are left unmerged, and you need to resolve the conflicts
 131and mark the resolved paths with `git add` (or `git rm` if the merge
 132should result in deletion of the path).
 133+
 134When checking out paths from the index, this option lets you recreate
 135the conflicted merge in the specified paths.
 136
 137--conflict=<style>::
 138        The same as --merge option above, but changes the way the
 139        conflicting hunks are presented, overriding the
 140        merge.conflictstyle configuration variable.  Possible values are
 141        "merge" (default) and "diff3" (in addition to what is shown by
 142        "merge" style, shows the original contents).
 143
 144-p::
 145--patch::
 146        Interactively select hunks in the difference between the
 147        <tree-ish> (or the index, if unspecified) and the working
 148        tree.  The chosen hunks are then applied in reverse to the
 149        working tree (and if a <tree-ish> was specified, the index).
 150+
 151This means that you can use `git checkout -p` to selectively discard
 152edits from your current working tree.
 153
 154<branch>::
 155        Branch to checkout; if it refers to a branch (i.e., a name that,
 156        when prepended with "refs/heads/", is a valid ref), then that
 157        branch is checked out. Otherwise, if it refers to a valid
 158        commit, your HEAD becomes "detached" and you are no longer on
 159        any branch (see below for details).
 160+
 161As a special case, the `"@\{-N\}"` syntax for the N-th last branch
 162checks out the branch (instead of detaching).  You may also specify
 163`-` which is synonymous with `"@\{-1\}"`.
 164
 165<new_branch>::
 166        Name for the new branch.
 167
 168<start_point>::
 169        The name of a commit at which to start the new branch; see
 170        linkgit:git-branch[1] for details. Defaults to HEAD.
 171
 172<tree-ish>::
 173        Tree to checkout from (when paths are given). If not specified,
 174        the index will be used.
 175
 176
 177
 178Detached HEAD
 179-------------
 180
 181It is sometimes useful to be able to 'checkout' a commit that is
 182not at the tip of one of your branches.  The most obvious
 183example is to check out the commit at a tagged official release
 184point, like this:
 185
 186------------
 187$ git checkout v2.6.18
 188------------
 189
 190Earlier versions of git did not allow this and asked you to
 191create a temporary branch using the `-b` option, but starting from
 192version 1.5.0, the above command 'detaches' your HEAD from the
 193current branch and directly points at the commit named by the tag
 194(`v2.6.18` in the example above).
 195
 196You can use all git commands while in this state.  You can use
 197`git reset --hard $othercommit` to further move around, for
 198example.  You can make changes and create a new commit on top of
 199a detached HEAD.  You can even create a merge by using `git
 200merge $othercommit`.
 201
 202The state you are in while your HEAD is detached is not recorded
 203by any branch (which is natural --- you are not on any branch).
 204What this means is that you can discard your temporary commits
 205and merges by switching back to an existing branch (e.g. `git
 206checkout master`), and a later `git prune` or `git gc` would
 207garbage-collect them.  If you did this by mistake, you can ask
 208the reflog for HEAD where you were, e.g.
 209
 210------------
 211$ git log -g -2 HEAD
 212------------
 213
 214
 215EXAMPLES
 216--------
 217
 218. The following sequence checks out the `master` branch, reverts
 219the `Makefile` to two revisions back, deletes hello.c by
 220mistake, and gets it back from the index.
 221+
 222------------
 223$ git checkout master             <1>
 224$ git checkout master~2 Makefile  <2>
 225$ rm -f hello.c
 226$ git checkout hello.c            <3>
 227------------
 228+
 229<1> switch branch
 230<2> take a file out of another commit
 231<3> restore hello.c from the index
 232+
 233If you have an unfortunate branch that is named `hello.c`, this
 234step would be confused as an instruction to switch to that branch.
 235You should instead write:
 236+
 237------------
 238$ git checkout -- hello.c
 239------------
 240
 241. After working in the wrong branch, switching to the correct
 242branch would be done using:
 243+
 244------------
 245$ git checkout mytopic
 246------------
 247+
 248However, your "wrong" branch and correct "mytopic" branch may
 249differ in files that you have modified locally, in which case
 250the above checkout would fail like this:
 251+
 252------------
 253$ git checkout mytopic
 254fatal: Entry 'frotz' not uptodate. Cannot merge.
 255------------
 256+
 257You can give the `-m` flag to the command, which would try a
 258three-way merge:
 259+
 260------------
 261$ git checkout -m mytopic
 262Auto-merging frotz
 263------------
 264+
 265After this three-way merge, the local modifications are _not_
 266registered in your index file, so `git diff` would show you what
 267changes you made since the tip of the new branch.
 268
 269. When a merge conflict happens during switching branches with
 270the `-m` option, you would see something like this:
 271+
 272------------
 273$ git checkout -m mytopic
 274Auto-merging frotz
 275ERROR: Merge conflict in frotz
 276fatal: merge program failed
 277------------
 278+
 279At this point, `git diff` shows the changes cleanly merged as in
 280the previous example, as well as the changes in the conflicted
 281files.  Edit and resolve the conflict and mark it resolved with
 282`git add` as usual:
 283+
 284------------
 285$ edit frotz
 286$ git add frotz
 287------------
 288
 289
 290Author
 291------
 292Written by Linus Torvalds <torvalds@osdl.org>
 293
 294Documentation
 295--------------
 296Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 297
 298GIT
 299---
 300Part of the linkgit:git[1] suite