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