blob: 8bdfa54ab09b4c4e8e4cb61767527e2ec06900b2 [file] [log] [blame]
Junio C Hamano215a7ad2005-09-07 17:26:23 -07001git-checkout(1)
2===============
Junio C Hamano7fc9d692005-08-23 01:49:47 -07003
4NAME
5----
Torsten Bögershausenc4ac5252015-06-17 09:54:51 +02006git-checkout - Switch branches or restore working tree files
Junio C Hamano7fc9d692005-08-23 01:49:47 -07007
8SYNOPSIS
9--------
Jon Loeliger71bb1032006-03-17 18:26:01 -060010[verse]
Jeff King76cfadf2009-04-13 07:19:33 -040011'git checkout' [-q] [-f] [-m] [<branch>]
Junio C Hamano26776c92013-09-11 10:05:17 -070012'git checkout' [-q] [-f] [-m] --detach [<branch>]
13'git checkout' [-q] [-f] [-m] [--detach] <commit>
Jean-Noël Avila133db542021-11-06 19:48:52 +010014'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
Junio C Hamano54f98fe2023-07-31 15:44:07 -070015'git checkout' [-f] <tree-ish> [--] <pathspec>...
16'git checkout' [-f] <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]
17'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...
18'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]
Alexandr Miloslavskiy8ea11892019-12-03 14:02:16 +000019'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
Junio C Hamano7fc9d692005-08-23 01:49:47 -070020
21DESCRIPTION
22-----------
Jonathan Niederb831ded2010-06-01 02:25:23 -050023Updates files in the working tree to match the version in the index
Alexandr Miloslavskiy8ea11892019-12-03 14:02:16 +000024or the specified tree. If no pathspec was given, 'git checkout' will
Jonathan Niederb831ded2010-06-01 02:25:23 -050025also update `HEAD` to set the specified branch as the current
Jeff King76cfadf2009-04-13 07:19:33 -040026branch.
Junio C Hamano4aaa7022005-10-18 01:29:27 -070027
Nguyễn Thái Ngọc Duy37f80022019-03-29 17:38:54 +070028'git checkout' [<branch>]::
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070029 To prepare for working on `<branch>`, switch to it by updating
Chris Rorvicke1cdf632012-12-17 00:45:01 -060030 the index and the files in the working tree, and by pointing
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070031 `HEAD` at the branch. Local modifications to the files in the
Chris Rorvicke1cdf632012-12-17 00:45:01 -060032 working tree are kept, so that they can be committed to the
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070033 `<branch>`.
Jonathan Niederc5b41512010-05-30 03:41:53 -050034+
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070035If `<branch>` is not found but there does exist a tracking branch in
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +070036exactly one remote (call it `<remote>`) with a matching name and
37`--no-guess` is not specified, treat as equivalent to
Chris Rorvick00bb4372012-12-17 00:45:02 -060038+
39------------
40$ git checkout -b <branch> --track <remote>/<branch>
41------------
42+
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070043You could omit `<branch>`, in which case the command degenerates to
Evan Zacksbe945682017-09-12 15:58:39 -070044"check out the current branch", which is a glorified no-op with
Chris Rorvicke1cdf632012-12-17 00:45:01 -060045rather expensive side-effects to show only the tracking information,
Elijah Newren89363522023-10-08 06:45:07 +000046if it exists, for the current branch.
Chris Rorvicke1cdf632012-12-17 00:45:01 -060047
Jean-Noël Avila133db542021-11-06 19:48:52 +010048'git checkout' -b|-B <new-branch> [<start-point>]::
Chris Rorvicke1cdf632012-12-17 00:45:01 -060049
50 Specifying `-b` causes a new branch to be created as if
51 linkgit:git-branch[1] were called and then checked out. In
52 this case you can use the `--track` or `--no-track` options,
53 which will be passed to 'git branch'. As a convenience,
54 `--track` without `-b` implies branch creation; see the
55 description of `--track` below.
Tay Ray Chuan02ac9832010-06-24 03:29:00 +080056+
Jean-Noël Avila133db542021-11-06 19:48:52 +010057If `-B` is given, `<new-branch>` is created if it doesn't exist; otherwise, it
Tay Ray Chuan02ac9832010-06-24 03:29:00 +080058is reset. This is the transactional equivalent of
59+
60------------
Jean-Noël Avila133db542021-11-06 19:48:52 +010061$ git branch -f <branch> [<start-point>]
Tay Ray Chuan02ac9832010-06-24 03:29:00 +080062$ git checkout <branch>
63------------
64+
65that is to say, the branch is not reset/created unless "git checkout" is
Junio C Hamanob23285a2023-11-23 15:00:31 +090066successful (e.g., when the branch is in use in another worktree, not
67just the current branch stays the same, but the branch is not reset to
68the start-point, either).
Junio C Hamano4aaa7022005-10-18 01:29:27 -070069
Chris Rorvicke1cdf632012-12-17 00:45:01 -060070'git checkout' --detach [<branch>]::
Junio C Hamano26776c92013-09-11 10:05:17 -070071'git checkout' [--detach] <commit>::
Chris Rorvicke1cdf632012-12-17 00:45:01 -060072
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070073 Prepare to work on top of `<commit>`, by detaching `HEAD` at it
Chris Rorvicke1cdf632012-12-17 00:45:01 -060074 (see "DETACHED HEAD" section), and updating the index and the
75 files in the working tree. Local modifications to the files
76 in the working tree are kept, so that the resulting working
77 tree will be the state recorded in the commit plus the local
78 modifications.
79+
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070080When the `<commit>` argument is a branch name, the `--detach` option can
81be used to detach `HEAD` at the tip of the branch (`git checkout
82<branch>` would check out that branch without detaching `HEAD`).
Junio C Hamano26776c92013-09-11 10:05:17 -070083+
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +070084Omitting `<branch>` detaches `HEAD` at the tip of the current branch.
Chris Rorvicke1cdf632012-12-17 00:45:01 -060085
Alexandr Miloslavskiy8ea11892019-12-03 14:02:16 +000086'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...::
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +000087'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]::
A Large Angry SCM0270f7c2005-09-07 17:17:18 -040088
Alexandr Miloslavskiy8ea11892019-12-03 14:02:16 +000089 Overwrite the contents of the files that match the pathspec.
90 When the `<tree-ish>` (most often a commit) is not given,
91 overwrite working tree with the contents in the index.
92 When the `<tree-ish>` is given, overwrite both the index and
93 the working tree with the contents at the `<tree-ish>`.
Torsten Bögershausenc4ac5252015-06-17 09:54:51 +020094+
Jonathan Niederb831ded2010-06-01 02:25:23 -050095The index may contain unmerged entries because of a previous failed merge.
96By default, if you try to check out such an entry from the index, the
Junio C Hamanodb941092008-08-30 07:46:55 -070097checkout operation will fail and nothing will be checked out.
Jonathan Niederb831ded2010-06-01 02:25:23 -050098Using `-f` will ignore these unmerged entries. The contents from a
Junio C Hamano38901a42008-08-30 07:48:18 -070099specific side of the merge can be checked out of the index by
Jonathan Niederb831ded2010-06-01 02:25:23 -0500100using `--ours` or `--theirs`. With `-m`, changes made to the working tree
101file can be discarded to re-create the original conflicted merge result.
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700102
Junio C Hamanob59698a2017-10-11 11:21:03 +0900103'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]::
Alexandr Miloslavskiy6fdc9ad2019-12-03 14:02:15 +0000104 This is similar to the previous mode, but lets you use the
105 interactive interface to show the "diff" output and choose which
106 hunks to use in the result. See below for the description of
107 `--patch` option.
Junio C Hamanob59698a2017-10-11 11:21:03 +0900108
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700109OPTIONS
110-------
Nicolas Pitre6124aee2007-02-01 12:31:26 -0500111-q::
René Scharfef7aec122009-08-29 11:05:00 +0200112--quiet::
Dave Watson2be7fcb2007-08-19 22:27:52 -0400113 Quiet, suppress feedback messages.
Nicolas Pitre6124aee2007-02-01 12:31:26 -0500114
Nguyễn Thái Ngọc Duyd333f672019-03-29 17:38:53 +0700115--progress::
116--no-progress::
Edmundo Carmona Antoranz870ebdb2015-11-01 15:19:05 -0600117 Progress status is reported on the standard error stream
118 by default when it is attached to a terminal, unless `--quiet`
119 is specified. This flag enables progress reporting even if not
120 attached to a terminal, regardless of `--quiet`.
121
A Large Angry SCM0270f7c2005-09-07 17:17:18 -0400122-f::
René Scharfef7aec122009-08-29 11:05:00 +0200123--force::
Junio C Hamanodb941092008-08-30 07:46:55 -0700124 When switching branches, proceed even if the index or the
Elijah Newren0e292222021-09-27 16:33:48 +0000125 working tree differs from `HEAD`, and even if there are untracked
126 files in the way. This is used to throw away local changes and
127 any untracked files or directories that are in the way.
Junio C Hamanodb941092008-08-30 07:46:55 -0700128+
129When checking out paths from the index, do not fail upon unmerged
130entries; instead, unmerged entries are ignored.
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700131
Junio C Hamano38901a42008-08-30 07:48:18 -0700132--ours::
133--theirs::
134 When checking out paths from the index, check out stage #2
135 ('ours') or #3 ('theirs') for unmerged paths.
Simon A. Eugsterf3030162015-07-10 13:07:11 -0700136+
137Note that during `git rebase` and `git pull --rebase`, 'ours' and
138'theirs' may appear swapped; `--ours` gives the version from the
139branch the changes are rebased onto, while `--theirs` gives the
140version from the branch that holds your work that is being rebased.
141+
142This is because `rebase` is used in a workflow that treats the
143history at the remote as the shared canonical one, and treats the
144work done on the branch you are rebasing as the third-party work to
145be integrated, and you are temporarily assuming the role of the
146keeper of the canonical history during the rebase. As the keeper of
147the canonical history, you need to view the history from the remote
148as `ours` (i.e. "our shared canonical history"), while what you did
149on your side branch as `theirs` (i.e. "one contributor's work on top
150of it").
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700151
Jean-Noël Avila133db542021-11-06 19:48:52 +0100152-b <new-branch>::
Junio C Hamanofedb8ea2023-01-19 09:12:41 -0800153 Create a new branch named `<new-branch>`, start it at
154 `<start-point>`, and check the resulting branch out;
155 see linkgit:git-branch[1] for details.
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700156
Jean-Noël Avila133db542021-11-06 19:48:52 +0100157-B <new-branch>::
Junio C Hamanofedb8ea2023-01-19 09:12:41 -0800158 Creates the branch `<new-branch>`, start it at `<start-point>`;
159 if it already exists, then reset it to `<start-point>`. And then
160 check the resulting branch out. This is equivalent to running
161 "git branch" with "-f" followed by "git checkout" of that branch;
162 see linkgit:git-branch[1] for details.
Tay Ray Chuan02ac9832010-06-24 03:29:00 +0800163
Stephan Beyer32402402008-06-08 03:36:09 +0200164-t::
René Scharfe6327f0e2022-01-20 13:35:54 +0100165--track[=(direct|inherit)]::
Jeff King26d22dc2009-04-13 07:18:52 -0400166 When creating a new branch, set up "upstream" configuration. See
167 "--track" in linkgit:git-branch[1] for details.
Johannes Schindelinbb0ceb62008-08-09 16:00:12 +0200168+
Matthieu Moy23f82392016-06-28 13:40:10 +0200169If no `-b` option is given, the name of the new branch will be
Johan Herlandfa83a332013-04-21 23:52:01 +0200170derived from the remote-tracking branch, by looking at the local part of
171the refspec configured for the corresponding remote, and then stripping
172the initial part up to the "*".
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700173This would tell us to use `hack` as the local branch when branching
174off of `origin/hack` (or `remotes/origin/hack`, or even
175`refs/remotes/origin/hack`). If the given name has no slash, or the above
Alex Riesen9188ed82008-08-21 19:23:20 +0200176guessing results in an empty name, the guessing is aborted. You can
Matthieu Moy23f82392016-06-28 13:40:10 +0200177explicitly give a name with `-b` in such a case.
Paolo Bonzini0746d192007-03-08 10:58:35 +0100178
179--no-track::
Jeff King167d7442009-04-13 07:11:56 -0400180 Do not set up "upstream" configuration, even if the
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700181 `branch.autoSetupMerge` configuration variable is true.
Paolo Bonzini0746d192007-03-08 10:58:35 +0100182
Nguyễn Thái Ngọc Duyccb111b2019-03-29 17:39:13 +0700183--guess::
184--no-guess::
185 If `<branch>` is not found but there does exist a tracking
186 branch in exactly one remote (call it `<remote>`) with a
187 matching name, treat as equivalent to
188+
189------------
190$ git checkout -b <branch> --track <remote>/<branch>
191------------
192+
193If the branch exists in multiple remotes and one of them is named by
194the `checkout.defaultRemote` configuration variable, we'll use that
195one for the purposes of disambiguation, even if the `<branch>` isn't
196unique across all remotes. Set it to
197e.g. `checkout.defaultRemote=origin` to always checkout remote
198branches from there if `<branch>` is ambiguous but exists on the
199'origin' remote. See also `checkout.defaultRemote` in
200linkgit:git-config[1].
201+
Denton Liu64f1f582020-10-07 22:48:15 -0700202`--guess` is the default behavior. Use `--no-guess` to disable it.
203+
204The default behavior can be set via the `checkout.guess` configuration
205variable.
Jon Loeliger71bb1032006-03-17 18:26:01 -0600206
Junio C Hamano1be06592006-01-12 14:04:36 -0800207-l::
Jeff King26d22dc2009-04-13 07:18:52 -0400208 Create the new branch's reflog; see linkgit:git-branch[1] for
209 details.
Junio C Hamano1be06592006-01-12 14:04:36 -0800210
Eric Sunshine07351d92020-09-06 20:02:20 -0400211-d::
Junio C Hamano32669672011-02-08 04:32:49 -0600212--detach::
213 Rather than checking out a branch to work on it, check out a
214 commit for inspection and discardable experiments.
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700215 This is the default behavior of `git checkout <commit>` when
216 `<commit>` is not a branch name. See the "DETACHED HEAD" section
Junio C Hamano32669672011-02-08 04:32:49 -0600217 below for details.
218
Jean-Noël Avila133db542021-11-06 19:48:52 +0100219--orphan <new-branch>::
Junio C Hamano49dc1562023-11-24 12:09:18 +0900220 Create a new unborn branch, named `<new-branch>`, started from
Jean-Noël Avila133db542021-11-06 19:48:52 +0100221 `<start-point>` and switch to it. The first commit made on this
Erick Mattosfeb98d12010-05-21 21:28:35 -0300222 new branch will have no parents and it will be the root of a new
223 history totally disconnected from all the other branches and
224 commits.
Erick Mattos9db5ebf2010-03-21 12:34:38 -0300225+
Erick Mattosfeb98d12010-05-21 21:28:35 -0300226The index and the working tree are adjusted as if you had previously run
Jean-Noël Avila133db542021-11-06 19:48:52 +0100227`git checkout <start-point>`. This allows you to start a new history
228that records a set of paths similar to `<start-point>` by easily running
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700229`git commit -a` to make the root commit.
Erick Mattos9db5ebf2010-03-21 12:34:38 -0300230+
Erick Mattosfeb98d12010-05-21 21:28:35 -0300231This can be useful when you want to publish the tree from a commit
232without exposing its full history. You might want to do this to publish
233an open source branch of a project whose current tree is "clean", but
234whose full history contains proprietary or otherwise encumbered bits of
235code.
236+
237If you want to start a disconnected history that records a set of paths
Jean-Noël Avila133db542021-11-06 19:48:52 +0100238that is totally different from the one of `<start-point>`, then you should
Erick Mattosfeb98d12010-05-21 21:28:35 -0300239clear the index and the working tree right after creating the orphan
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700240branch by running `git rm -rf .` from the top level of the working tree.
Erick Mattosfeb98d12010-05-21 21:28:35 -0300241Afterwards you will be ready to prepare your new files, repopulating the
242working tree, by copying them from elsewhere, extracting a tarball, etc.
Erick Mattos9db5ebf2010-03-21 12:34:38 -0300243
Nguyễn Thái Ngọc Duy08d595d2013-04-13 09:12:08 +1000244--ignore-skip-worktree-bits::
245 In sparse checkout mode, `git checkout -- <paths>` would
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700246 update only entries matched by `<paths>` and sparse patterns
247 in `$GIT_DIR/info/sparse-checkout`. This option ignores
248 the sparse patterns and adds back any files in `<paths>`.
Nguyễn Thái Ngọc Duy08d595d2013-04-13 09:12:08 +1000249
Junio C Hamano1be06592006-01-12 14:04:36 -0800250-m::
Junio C Hamanoeac5a402008-08-31 19:32:40 -0700251--merge::
Junio C Hamano0cf85812008-08-30 07:52:24 -0700252 When switching branches,
253 if you have local modifications to one or more files that
Junio C Hamano1be06592006-01-12 14:04:36 -0800254 are different between the current branch and the branch to
255 which you are switching, the command refuses to switch
256 branches in order to preserve your modifications in context.
257 However, with this option, a three-way merge between the current
258 branch, your working tree contents, and the new branch
259 is done, and you will be on the new branch.
260+
261When a merge conflict happens, the index entries for conflicting
262paths are left unmerged, and you need to resolve the conflicts
Shawn O. Pearced7f078b2007-02-17 04:43:42 -0500263and mark the resolved paths with `git add` (or `git rm` if the merge
264should result in deletion of the path).
Junio C Hamano0cf85812008-08-30 07:52:24 -0700265+
266When checking out paths from the index, this option lets you recreate
Junio C Hamano54f98fe2023-07-31 15:44:07 -0700267the conflicted merge in the specified paths. This option cannot be
268used when checking out paths from a tree-ish.
Nguyễn Thái Ngọc Duya7256de2019-03-19 16:39:10 +0700269+
270When switching branches with `--merge`, staged changes may be lost.
Junio C Hamano1be06592006-01-12 14:04:36 -0800271
Junio C Hamanoeac5a402008-08-31 19:32:40 -0700272--conflict=<style>::
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700273 The same as `--merge` option above, but changes the way the
Junio C Hamanoeac5a402008-08-31 19:32:40 -0700274 conflicting hunks are presented, overriding the
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700275 `merge.conflictStyle` configuration variable. Possible values are
Elijah Newrenddfc44a82021-12-01 00:05:07 +0000276 "merge" (default), "diff3", and "zdiff3".
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700277
Thomas Rast4f353652009-08-15 13:48:30 +0200278-p::
279--patch::
280 Interactively select hunks in the difference between the
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700281 `<tree-ish>` (or the index, if unspecified) and the working
Thomas Rast4f353652009-08-15 13:48:30 +0200282 tree. The chosen hunks are then applied in reverse to the
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700283 working tree (and if a `<tree-ish>` was specified, the index).
Thomas Rast4f353652009-08-15 13:48:30 +0200284+
285This means that you can use `git checkout -p` to selectively discard
Valentin Haenela31538e2011-05-05 20:48:48 +0200286edits from your current working tree. See the ``Interactive Mode''
Jeff King6cf378f2012-04-26 04:51:57 -0400287section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000288+
289Note that this option uses the no overlay mode by default (see also
Nguyễn Thái Ngọc Duyd333f672019-03-29 17:38:53 +0700290`--overlay`), and currently doesn't support overlay mode.
Thomas Rast4f353652009-08-15 13:48:30 +0200291
Nguyễn Thái Ngọc Duy1d0fa892015-01-03 16:41:26 +0700292--ignore-other-worktrees::
293 `git checkout` refuses when the wanted ref is already checked
294 out by another worktree. This option makes it check the ref
295 out anyway. In other words, the ref can be held by more than one
296 worktree.
297
Nguyễn Thái Ngọc Duy9d223d42019-03-29 17:38:55 +0700298--overwrite-ignore::
299--no-overwrite-ignore::
300 Silently overwrite ignored files when switching branches. This
301 is the default behavior. Use `--no-overwrite-ignore` to abort
302 the operation when the new branch contains ignored files.
303
Nguyễn Thái Ngọc Duyd333f672019-03-29 17:38:53 +0700304--recurse-submodules::
305--no-recurse-submodules::
Damien Robertacbfae32020-04-06 15:57:09 +0200306 Using `--recurse-submodules` will update the content of all active
Stefan Beller1fc458d2017-03-14 14:46:41 -0700307 submodules according to the commit recorded in the superproject. If
308 local modifications in a submodule would be overwritten the checkout
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700309 will fail unless `-f` is used. If nothing (or `--no-recurse-submodules`)
Damien Robertb3cec572020-04-06 15:57:06 +0200310 is used, submodules working trees will not be updated.
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700311 Just like linkgit:git-submodule[1], this will detach `HEAD` of the
312 submodule.
Stefan Beller1fc458d2017-03-14 14:46:41 -0700313
Nguyễn Thái Ngọc Duyd333f672019-03-29 17:38:53 +0700314--overlay::
315--no-overlay::
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000316 In the default overlay mode, `git checkout` never
317 removes files from the index or the working tree. When
318 specifying `--no-overlay`, files that appear in the index and
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700319 working tree, but not in `<tree-ish>` are removed, to make them
320 match `<tree-ish>` exactly.
Thomas Gummerer091e04b2019-01-08 21:52:24 +0000321
Alexandr Miloslavskiya9aecc72019-12-03 14:02:18 +0000322--pathspec-from-file=<file>::
323 Pathspec is passed in `<file>` instead of commandline args. If
324 `<file>` is exactly `-` then standard input is used. Pathspec
325 elements are separated by LF or CR/LF. Pathspec elements can be
326 quoted as explained for the configuration variable `core.quotePath`
327 (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
328 global `--literal-pathspecs`.
329
330--pathspec-file-nul::
331 Only meaningful with `--pathspec-from-file`. Pathspec elements are
332 separated with NUL character and all other characters are taken
333 literally (including newlines and quotes).
334
A Large Angry SCM0270f7c2005-09-07 17:17:18 -0400335<branch>::
Jeff King08087232009-04-13 07:21:04 -0400336 Branch to checkout; if it refers to a branch (i.e., a name that,
337 when prepended with "refs/heads/", is a valid ref), then that
338 branch is checked out. Otherwise, if it refers to a valid
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700339 commit, your `HEAD` becomes "detached" and you are no longer on
Jeff King08087232009-04-13 07:21:04 -0400340 any branch (see below for details).
Thomas Rast696acf42009-01-17 17:09:56 +0100341+
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700342You can use the `@{-N}` syntax to refer to the N-th last
Kaartic Sivaraam75ce1492017-11-27 22:58:33 +0530343branch/commit checked out using "git checkout" operation. You may
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700344also specify `-` which is synonymous to `@{-1}`.
Michael J Gruber873c3472010-06-01 17:16:42 +0200345+
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700346As a special case, you may use `A...B` as a shortcut for the
Michael J Gruber873c3472010-06-01 17:16:42 +0200347merge base of `A` and `B` if there is exactly one merge base. You can
348leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
Junio C Hamano5e1a2e82007-01-17 10:43:50 -0800349
Jean-Noël Avila133db542021-11-06 19:48:52 +0100350<new-branch>::
Jeff King76cfadf2009-04-13 07:19:33 -0400351 Name for the new branch.
352
Jean-Noël Avila133db542021-11-06 19:48:52 +0100353<start-point>::
Jeff King76cfadf2009-04-13 07:19:33 -0400354 The name of a commit at which to start the new branch; see
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700355 linkgit:git-branch[1] for details. Defaults to `HEAD`.
Denton Liue3d65392019-04-27 05:02:22 -0700356+
357As a special case, you may use `"A...B"` as a shortcut for the
358merge base of `A` and `B` if there is exactly one merge base. You can
359leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
Jeff King76cfadf2009-04-13 07:19:33 -0400360
361<tree-ish>::
362 Tree to checkout from (when paths are given). If not specified,
363 the index will be used.
Denton Liuc693ef72020-10-07 00:56:16 -0700364+
365As a special case, you may use `"A...B"` as a shortcut for the
366merge base of `A` and `B` if there is exactly one merge base. You can
367leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
Jeff King76cfadf2009-04-13 07:19:33 -0400368
Alexandr Miloslavskiy8ea11892019-12-03 14:02:16 +0000369\--::
370 Do not interpret any more arguments as options.
Jeff King76cfadf2009-04-13 07:19:33 -0400371
Alexandr Miloslavskiy8ea11892019-12-03 14:02:16 +0000372<pathspec>...::
373 Limits the paths affected by the operation.
374+
375For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
Junio C Hamano5e1a2e82007-01-17 10:43:50 -0800376
Junio C Hamano32669672011-02-08 04:32:49 -0600377DETACHED HEAD
Junio C Hamano5e1a2e82007-01-17 10:43:50 -0800378-------------
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700379`HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each
Jay Soffianbe8ef332011-02-20 00:21:50 -0500380branch refers to a specific commit. Let's look at a repo with three
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700381commits, one of them tagged, and with branch `master` checked out:
Junio C Hamano5e1a2e82007-01-17 10:43:50 -0800382
383------------
Andreas Heiduk39a36822018-10-22 22:45:42 +0200384 HEAD (refers to branch 'master')
385 |
386 v
Jay Soffianbe8ef332011-02-20 00:21:50 -0500387a---b---c branch 'master' (refers to commit 'c')
388 ^
389 |
390 tag 'v2.0' (refers to commit 'b')
Junio C Hamano5e1a2e82007-01-17 10:43:50 -0800391------------
392
Jay Soffianbe8ef332011-02-20 00:21:50 -0500393When a commit is created in this state, the branch is updated to refer to
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700394the new commit. Specifically, 'git commit' creates a new commit `d`, whose
395parent is commit `c`, and then updates branch `master` to refer to new
396commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers
397to commit `d`:
Junio C Hamano5e1a2e82007-01-17 10:43:50 -0800398
Junio C Hamanocec8d142007-02-13 08:58:01 -0800399------------
Jay Soffianbe8ef332011-02-20 00:21:50 -0500400$ edit; git add; git commit
401
Andreas Heiduk39a36822018-10-22 22:45:42 +0200402 HEAD (refers to branch 'master')
403 |
404 v
Jay Soffianbe8ef332011-02-20 00:21:50 -0500405a---b---c---d branch 'master' (refers to commit 'd')
406 ^
407 |
408 tag 'v2.0' (refers to commit 'b')
409------------
410
411It is sometimes useful to be able to checkout a commit that is not at
412the tip of any named branch, or even to create a new commit that is not
413referenced by a named branch. Let's look at what happens when we
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700414checkout commit `b` (here we show two ways this may be done):
Jay Soffianbe8ef332011-02-20 00:21:50 -0500415
416------------
417$ git checkout v2.0 # or
418$ git checkout master^^
419
420 HEAD (refers to commit 'b')
421 |
422 v
423a---b---c---d branch 'master' (refers to commit 'd')
424 ^
425 |
426 tag 'v2.0' (refers to commit 'b')
427------------
428
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700429Notice that regardless of which checkout command we use, `HEAD` now refers
430directly to commit `b`. This is known as being in detached `HEAD` state.
431It means simply that `HEAD` refers to a specific commit, as opposed to
Jay Soffianbe8ef332011-02-20 00:21:50 -0500432referring to a named branch. Let's see what happens when we create a commit:
433
434------------
435$ edit; git add; git commit
436
437 HEAD (refers to commit 'e')
438 |
439 v
440 e
441 /
442a---b---c---d branch 'master' (refers to commit 'd')
443 ^
444 |
445 tag 'v2.0' (refers to commit 'b')
446------------
447
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700448There is now a new commit `e`, but it is referenced only by `HEAD`. We can
Jay Soffianbe8ef332011-02-20 00:21:50 -0500449of course add yet another commit in this state:
450
451------------
452$ edit; git add; git commit
453
454 HEAD (refers to commit 'f')
455 |
456 v
457 e---f
458 /
459a---b---c---d branch 'master' (refers to commit 'd')
460 ^
461 |
462 tag 'v2.0' (refers to commit 'b')
463------------
464
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100465In fact, we can perform all the normal Git operations. But, let's look
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700466at what happens when we then checkout `master`:
Jay Soffianbe8ef332011-02-20 00:21:50 -0500467
468------------
469$ git checkout master
470
Andreas Heiduk39a36822018-10-22 22:45:42 +0200471 HEAD (refers to branch 'master')
Jay Soffianbe8ef332011-02-20 00:21:50 -0500472 e---f |
473 / v
474a---b---c---d branch 'master' (refers to commit 'd')
475 ^
476 |
477 tag 'v2.0' (refers to commit 'b')
478------------
479
480It is important to realize that at this point nothing refers to commit
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700481`f`. Eventually commit `f` (and by extension commit `e`) will be deleted
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100482by the routine Git garbage collection process, unless we create a reference
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700483before that happens. If we have not yet moved away from commit `f`,
Jay Soffianbe8ef332011-02-20 00:21:50 -0500484any of these will create a reference to it:
485
486------------
Yutaro Ohno9e379692023-01-09 10:47:17 +0000487$ git checkout -b foo # or "git switch -c foo" <1>
488$ git branch foo <2>
489$ git tag foo <3>
Jay Soffianbe8ef332011-02-20 00:21:50 -0500490------------
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700491<1> creates a new branch `foo`, which refers to commit `f`, and then
492 updates `HEAD` to refer to branch `foo`. In other words, we'll no longer
493 be in detached `HEAD` state after this command.
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700494<2> similarly creates a new branch `foo`, which refers to commit `f`,
495 but leaves `HEAD` detached.
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700496<3> creates a new tag `foo`, which refers to commit `f`,
497 leaving `HEAD` detached.
Jay Soffianbe8ef332011-02-20 00:21:50 -0500498
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700499If we have moved away from commit `f`, then we must first recover its object
Jay Soffianbe8ef332011-02-20 00:21:50 -0500500name (typically by using git reflog), and then we can create a reference to
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700501it. For example, to see the last two commits to which `HEAD` referred, we
Jay Soffianbe8ef332011-02-20 00:21:50 -0500502can use either of these commands:
503
504------------
505$ git reflog -2 HEAD # or
Junio C Hamanocec8d142007-02-13 08:58:01 -0800506$ git log -g -2 HEAD
507------------
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700508
Nguyễn Thái Ngọc Duy19e56562016-09-07 18:19:40 +0700509ARGUMENT DISAMBIGUATION
510-----------------------
511
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700512When there is only one argument given and it is not `--` (e.g. `git
513checkout abc`), and when the argument is both a valid `<tree-ish>`
514(e.g. a branch `abc` exists) and a valid `<pathspec>` (e.g. a file
Nguyễn Thái Ngọc Duy19e56562016-09-07 18:19:40 +0700515or a directory whose name is "abc" exists), Git would usually ask
516you to disambiguate. Because checking out a branch is so common an
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700517operation, however, `git checkout abc` takes "abc" as a `<tree-ish>`
Nguyễn Thái Ngọc Duy19e56562016-09-07 18:19:40 +0700518in such a situation. Use `git checkout -- <pathspec>` if you want
519to checkout these paths out of the index.
520
Junio C Hamano1be06592006-01-12 14:04:36 -0800521EXAMPLES
522--------
Junio C Hamano4aaa7022005-10-18 01:29:27 -0700523
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600524=== 1. Paths
525
526The following sequence checks out the `master` branch, reverts
527the `Makefile` to two revisions back, deletes `hello.c` by
528mistake, and gets it back from the index.
529
Junio C Hamano4aaa7022005-10-18 01:29:27 -0700530------------
Sean Estabrooks48aeecd2006-04-28 09:15:05 -0400531$ git checkout master <1>
532$ git checkout master~2 Makefile <2>
Junio C Hamano4aaa7022005-10-18 01:29:27 -0700533$ rm -f hello.c
Sean Estabrooks48aeecd2006-04-28 09:15:05 -0400534$ git checkout hello.c <3>
Junio C Hamano4aaa7022005-10-18 01:29:27 -0700535------------
Sean Estabrooks48aeecd2006-04-28 09:15:05 -0400536<1> switch branch
Michael J Gruberc7cb12b2009-03-10 16:06:30 +0100537<2> take a file out of another commit
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700538<3> restore `hello.c` from the index
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600539
Junio C Hamanocaae3192012-09-04 08:28:27 -0700540If you want to check out _all_ C source files out of the index,
541you can say
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600542
Junio C Hamanocaae3192012-09-04 08:28:27 -0700543------------
544$ git checkout -- '*.c'
545------------
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600546
Junio C Hamanocaae3192012-09-04 08:28:27 -0700547Note the quotes around `*.c`. The file `hello.c` will also be
548checked out, even though it is no longer in the working tree,
549because the file globbing is used to match entries in the index
550(not in the working tree by the shell).
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600551
Sean Estabrooks48aeecd2006-04-28 09:15:05 -0400552If you have an unfortunate branch that is named `hello.c`, this
553step would be confused as an instruction to switch to that branch.
554You should instead write:
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600555
Junio C Hamano4aaa7022005-10-18 01:29:27 -0700556------------
557$ git checkout -- hello.c
558------------
559
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600560=== 2. Merge
561
562After working in the wrong branch, switching to the correct
563branch would be done using:
564
Junio C Hamano1be06592006-01-12 14:04:36 -0800565------------
566$ git checkout mytopic
567------------
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600568
Nguyễn Thái Ngọc Duy181e3722019-03-29 17:38:56 +0700569However, your "wrong" branch and correct `mytopic` branch may
Michael J Gruberc7cb12b2009-03-10 16:06:30 +0100570differ in files that you have modified locally, in which case
Junio C Hamano1be06592006-01-12 14:04:36 -0800571the above checkout would fail like this:
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600572
Junio C Hamano1be06592006-01-12 14:04:36 -0800573------------
574$ git checkout mytopic
Nicolas Sebrecht142183d2010-07-09 22:27:48 +0200575error: You have local changes to 'frotz'; not switching branches.
Junio C Hamano1be06592006-01-12 14:04:36 -0800576------------
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600577
Junio C Hamano1be06592006-01-12 14:04:36 -0800578You can give the `-m` flag to the command, which would try a
579three-way merge:
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600580
Junio C Hamano1be06592006-01-12 14:04:36 -0800581------------
582$ git checkout -m mytopic
583Auto-merging frotz
584------------
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600585
Junio C Hamano1be06592006-01-12 14:04:36 -0800586After this three-way merge, the local modifications are _not_
587registered in your index file, so `git diff` would show you what
588changes you made since the tip of the new branch.
589
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600590=== 3. Merge conflict
591
592When a merge conflict happens during switching branches with
593the `-m` option, you would see something like this:
594
Junio C Hamano1be06592006-01-12 14:04:36 -0800595------------
596$ git checkout -m mytopic
597Auto-merging frotz
Junio C Hamano1be06592006-01-12 14:04:36 -0800598ERROR: Merge conflict in frotz
599fatal: merge program failed
600------------
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600601
Junio C Hamano1be06592006-01-12 14:04:36 -0800602At this point, `git diff` shows the changes cleanly merged as in
603the previous example, as well as the changes in the conflicted
604files. Edit and resolve the conflict and mark it resolved with
Shawn O. Pearced7f078b2007-02-17 04:43:42 -0500605`git add` as usual:
Felipe Contreras8dda6c32023-04-18 01:00:48 -0600606
Junio C Hamano1be06592006-01-12 14:04:36 -0800607------------
608$ edit frotz
Shawn O. Pearced7f078b2007-02-17 04:43:42 -0500609$ git add frotz
Junio C Hamano1be06592006-01-12 14:04:36 -0800610------------
611
Ævar Arnfjörð Bjarmason9274dea2022-09-07 10:27:05 +0200612CONFIGURATION
613-------------
614
615include::includes/cmd-config-section-all.txt[]
616
617include::config/checkout.txt[]
618
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +0700619SEE ALSO
620--------
Nguyễn Thái Ngọc Duy46e91b62019-04-25 16:45:45 +0700621linkgit:git-switch[1],
622linkgit:git-restore[1]
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +0700623
Junio C Hamano7fc9d692005-08-23 01:49:47 -0700624GIT
625---
Christian Couder9e1f0a82008-06-06 09:07:32 +0200626Part of the linkgit:git[1] suite