Documentation / git-checkout.txton commit Make fetch a builtin (b888d61)
   1git-checkout(1)
   2===============
   3
   4NAME
   5----
   6git-checkout - Checkout and switch to a branch
   7
   8SYNOPSIS
   9--------
  10[verse]
  11'git-checkout' [-q] [-f] [[--track | --no-track] -b <new_branch> [-l]] [-m] [<branch>]
  12'git-checkout' [<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
  24When <paths> are given, this command does *not* switch
  25branches.  It updates the named paths in the working tree from
  26the index file (i.e. it runs `git-checkout-index -f -u`), or
  27from a named commit.  In
  28this case, the `-f` and `-b` options are meaningless and giving
  29either of them results in an error.  <tree-ish> argument can be
  30used to specify a specific tree-ish (i.e. commit, tag or tree)
  31to update the index for the given paths before updating the
  32working tree.
  33
  34
  35OPTIONS
  36-------
  37-q::
  38        Quiet, suppress feedback messages.
  39
  40-f::
  41        Proceed even if the index or the working tree differs
  42        from HEAD.  This is used to throw away local changes.
  43
  44-b::
  45        Create a new branch named <new_branch> and start it at
  46        <branch>.  The new branch name must pass all checks defined
  47        by gitlink:git-check-ref-format[1].  Some of these checks
  48        may restrict the characters allowed in a branch name.
  49
  50--track::
  51        When -b is given and a branch is created off a remote branch,
  52        set up configuration so that git-pull will automatically
  53        retrieve data from the remote branch.  Set the
  54        branch.autosetupmerge configuration variable to true if you
  55        want git-checkout and git-branch to always behave as if
  56        '--track' were given.
  57
  58--no-track::
  59        When -b is given and a branch is created off a remote branch,
  60        set up configuration so that git-pull will not retrieve data
  61        from the remote branch, ignoring the branch.autosetupmerge
  62        configuration variable.
  63
  64-l::
  65        Create the new branch's reflog.  This activates recording of
  66        all changes made to the branch ref, enabling use of date
  67        based sha1 expressions such as "<branchname>@\{yesterday}".
  68
  69-m::
  70        If you have local modifications to one or more files that
  71        are different between the current branch and the branch to
  72        which you are switching, the command refuses to switch
  73        branches in order to preserve your modifications in context.
  74        However, with this option, a three-way merge between the current
  75        branch, your working tree contents, and the new branch
  76        is done, and you will be on the new branch.
  77+
  78When a merge conflict happens, the index entries for conflicting
  79paths are left unmerged, and you need to resolve the conflicts
  80and mark the resolved paths with `git add` (or `git rm` if the merge
  81should result in deletion of the path).
  82
  83<new_branch>::
  84        Name for the new branch.
  85
  86<branch>::
  87        Branch to checkout; may be any object ID that resolves to a
  88        commit.  Defaults to HEAD.
  89+
  90When this parameter names a non-branch (but still a valid commit object),
  91your HEAD becomes 'detached'.
  92
  93
  94Detached HEAD
  95-------------
  96
  97It is sometimes useful to be able to 'checkout' a commit that is
  98not at the tip of one of your branches.  The most obvious
  99example is to check out the commit at a tagged official release
 100point, like this:
 101
 102------------
 103$ git checkout v2.6.18
 104------------
 105
 106Earlier versions of git did not allow this and asked you to
 107create a temporary branch using `-b` option, but starting from
 108version 1.5.0, the above command 'detaches' your HEAD from the
 109current branch and directly point at the commit named by the tag
 110(`v2.6.18` in the above example).
 111
 112You can use usual git commands while in this state.  You can use
 113`git-reset --hard $othercommit` to further move around, for
 114example.  You can make changes and create a new commit on top of
 115a detached HEAD.  You can even create a merge by using `git
 116merge $othercommit`.
 117
 118The state you are in while your HEAD is detached is not recorded
 119by any branch (which is natural --- you are not on any branch).
 120What this means is that you can discard your temporary commits
 121and merges by switching back to an existing branch (e.g. `git
 122checkout master`), and a later `git prune` or `git gc` would
 123garbage-collect them.  If you did this by mistake, you can ask
 124the reflog for HEAD where you were, e.g.
 125
 126------------
 127$ git log -g -2 HEAD
 128------------
 129
 130
 131EXAMPLES
 132--------
 133
 134. The following sequence checks out the `master` branch, reverts
 135the `Makefile` to two revisions back, deletes hello.c by
 136mistake, and gets it back from the index.
 137+
 138------------
 139$ git checkout master             <1>
 140$ git checkout master~2 Makefile  <2>
 141$ rm -f hello.c
 142$ git checkout hello.c            <3>
 143------------
 144+
 145<1> switch branch
 146<2> take out a file out of other commit
 147<3> restore hello.c from HEAD of current branch
 148+
 149If you have an unfortunate branch that is named `hello.c`, this
 150step would be confused as an instruction to switch to that branch.
 151You should instead write:
 152+
 153------------
 154$ git checkout -- hello.c
 155------------
 156
 157. After working in a wrong branch, switching to the correct
 158branch would be done using:
 159+
 160------------
 161$ git checkout mytopic
 162------------
 163+
 164However, your "wrong" branch and correct "mytopic" branch may
 165differ in files that you have locally modified, in which case,
 166the above checkout would fail like this:
 167+
 168------------
 169$ git checkout mytopic
 170fatal: Entry 'frotz' not uptodate. Cannot merge.
 171------------
 172+
 173You can give the `-m` flag to the command, which would try a
 174three-way merge:
 175+
 176------------
 177$ git checkout -m mytopic
 178Auto-merging frotz
 179------------
 180+
 181After this three-way merge, the local modifications are _not_
 182registered in your index file, so `git diff` would show you what
 183changes you made since the tip of the new branch.
 184
 185. When a merge conflict happens during switching branches with
 186the `-m` option, you would see something like this:
 187+
 188------------
 189$ git checkout -m mytopic
 190Auto-merging frotz
 191merge: warning: conflicts during merge
 192ERROR: Merge conflict in frotz
 193fatal: merge program failed
 194------------
 195+
 196At this point, `git diff` shows the changes cleanly merged as in
 197the previous example, as well as the changes in the conflicted
 198files.  Edit and resolve the conflict and mark it resolved with
 199`git add` as usual:
 200+
 201------------
 202$ edit frotz
 203$ git add frotz
 204------------
 205
 206
 207Author
 208------
 209Written by Linus Torvalds <torvalds@osdl.org>
 210
 211Documentation
 212--------------
 213Documentation by Junio C Hamano and the git-list <git@vger.kernel.org>.
 214
 215GIT
 216---
 217Part of the gitlink:git[7] suite