Documentation / git-checkout.txton commit stash: simplify defaulting to "save" and reject unknown options (3c2eb80)
   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 <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, 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        <start_point>; 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-p::
 118--patch::
 119        Interactively select hunks in the difference between the
 120        <tree-ish> (or the index, if unspecified) and the working
 121        tree.  The chosen hunks are then applied in reverse to the
 122        working tree (and if a <tree-ish> was specified, the index).
 123+
 124This means that you can use `git checkout -p` to selectively discard
 125edits from your current working tree.
 126
 127<branch>::
 128        Branch to checkout; if it refers to a branch (i.e., a name that,
 129        when prepended with "refs/heads/", is a valid ref), then that
 130        branch is checked out. Otherwise, if it refers to a valid
 131        commit, your HEAD becomes "detached" and you are no longer on
 132        any branch (see below for details).
 133+
 134As a special case, the `"@\{-N\}"` syntax for the N-th last branch
 135checks out the branch (instead of detaching).  You may also specify
 136`-` which is synonymous with `"@\{-1\}"`.
 137
 138<new_branch>::
 139        Name for the new branch.
 140
 141<start_point>::
 142        The name of a commit at which to start the new branch; see
 143        linkgit:git-branch[1] for details. Defaults to HEAD.
 144
 145<tree-ish>::
 146        Tree to checkout from (when paths are given). If not specified,
 147        the index will be used.
 148
 149
 150
 151Detached HEAD
 152-------------
 153
 154It is sometimes useful to be able to 'checkout' a commit that is
 155not at the tip of one of your branches.  The most obvious
 156example is to check out the commit at a tagged official release
 157point, like this:
 158
 159------------
 160$ git checkout v2.6.18
 161------------
 162
 163Earlier versions of git did not allow this and asked you to
 164create a temporary branch using the `-b` option, but starting from
 165version 1.5.0, the above command 'detaches' your HEAD from the
 166current branch and directly points at the commit named by the tag
 167(`v2.6.18` in the example above).
 168
 169You can use all git commands while in this state.  You can use
 170`git reset --hard $othercommit` to further move around, for
 171example.  You can make changes and create a new commit on top of
 172a detached HEAD.  You can even create a merge by using `git
 173merge $othercommit`.
 174
 175The state you are in while your HEAD is detached is not recorded
 176by any branch (which is natural --- you are not on any branch).
 177What this means is that you can discard your temporary commits
 178and merges by switching back to an existing branch (e.g. `git
 179checkout master`), and a later `git prune` or `git gc` would
 180garbage-collect them.  If you did this by mistake, you can ask
 181the reflog for HEAD where you were, e.g.
 182
 183------------
 184$ git log -g -2 HEAD
 185------------
 186
 187
 188EXAMPLES
 189--------
 190
 191. The following sequence checks out the `master` branch, reverts
 192the `Makefile` to two revisions back, deletes hello.c by
 193mistake, and gets it back from the index.
 194+
 195------------
 196$ git checkout master             <1>
 197$ git checkout master~2 Makefile  <2>
 198$ rm -f hello.c
 199$ git checkout hello.c            <3>
 200------------
 201+
 202<1> switch branch
 203<2> take a file out of another commit
 204<3> restore hello.c from the index
 205+
 206If you have an unfortunate branch that is named `hello.c`, this
 207step would be confused as an instruction to switch to that branch.
 208You should instead write:
 209+
 210------------
 211$ git checkout -- hello.c
 212------------
 213
 214. After working in the wrong branch, switching to the correct
 215branch would be done using:
 216+
 217------------
 218$ git checkout mytopic
 219------------
 220+
 221However, your "wrong" branch and correct "mytopic" branch may
 222differ in files that you have modified locally, in which case
 223the above checkout would fail like this:
 224+
 225------------
 226$ git checkout mytopic
 227fatal: Entry 'frotz' not uptodate. Cannot merge.
 228------------
 229+
 230You can give the `-m` flag to the command, which would try a
 231three-way merge:
 232+
 233------------
 234$ git checkout -m mytopic
 235Auto-merging frotz
 236------------
 237+
 238After this three-way merge, the local modifications are _not_
 239registered in your index file, so `git diff` would show you what
 240changes you made since the tip of the new branch.
 241
 242. When a merge conflict happens during switching branches with
 243the `-m` option, you would see something like this:
 244+
 245------------
 246$ git checkout -m mytopic
 247Auto-merging frotz
 248ERROR: Merge conflict in frotz
 249fatal: merge program failed
 250------------
 251+
 252At this point, `git diff` shows the changes cleanly merged as in
 253the previous example, as well as the changes in the conflicted
 254files.  Edit and resolve the conflict and mark it resolved with
 255`git add` as usual:
 256+
 257------------
 258$ edit frotz
 259$ git add frotz
 260------------
 261
 262
 263Author
 264------
 265Written by Linus Torvalds <torvalds@osdl.org>
 266
 267Documentation
 268--------------
 269Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 270
 271GIT
 272---
 273Part of the linkgit:git[1] suite