Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 1 | git-checkout(1) |
| 2 | =============== |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 3 | |
| 4 | NAME |
| 5 | ---- |
Torsten Bögershausen | c4ac525 | 2015-06-17 09:54:51 +0200 | [diff] [blame] | 6 | git-checkout - Switch branches or restore working tree files |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
Jon Loeliger | 71bb103 | 2006-03-17 18:26:01 -0600 | [diff] [blame] | 10 | [verse] |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 11 | 'git checkout' [-q] [-f] [-m] [<branch>] |
Junio C Hamano | 26776c9 | 2013-09-11 10:05:17 -0700 | [diff] [blame] | 12 | 'git checkout' [-q] [-f] [-m] --detach [<branch>] |
| 13 | 'git checkout' [-q] [-f] [-m] [--detach] <commit> |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 14 | 'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>] |
Junio C Hamano | 54f98fe | 2023-07-31 15:44:07 -0700 | [diff] [blame] | 15 | '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 Miloslavskiy | 8ea1189 | 2019-12-03 14:02:16 +0000 | [diff] [blame] | 19 | 'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...] |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 20 | |
| 21 | DESCRIPTION |
| 22 | ----------- |
Jonathan Nieder | b831ded | 2010-06-01 02:25:23 -0500 | [diff] [blame] | 23 | Updates files in the working tree to match the version in the index |
Alexandr Miloslavskiy | 8ea1189 | 2019-12-03 14:02:16 +0000 | [diff] [blame] | 24 | or the specified tree. If no pathspec was given, 'git checkout' will |
Jonathan Nieder | b831ded | 2010-06-01 02:25:23 -0500 | [diff] [blame] | 25 | also update `HEAD` to set the specified branch as the current |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 26 | branch. |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 27 | |
Nguyễn Thái Ngọc Duy | 37f8002 | 2019-03-29 17:38:54 +0700 | [diff] [blame] | 28 | 'git checkout' [<branch>]:: |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 29 | To prepare for working on `<branch>`, switch to it by updating |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 30 | the index and the files in the working tree, and by pointing |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 31 | `HEAD` at the branch. Local modifications to the files in the |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 32 | working tree are kept, so that they can be committed to the |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 33 | `<branch>`. |
Jonathan Nieder | c5b4151 | 2010-05-30 03:41:53 -0500 | [diff] [blame] | 34 | + |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 35 | If `<branch>` is not found but there does exist a tracking branch in |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 36 | exactly one remote (call it `<remote>`) with a matching name and |
| 37 | `--no-guess` is not specified, treat as equivalent to |
Chris Rorvick | 00bb437 | 2012-12-17 00:45:02 -0600 | [diff] [blame] | 38 | + |
| 39 | ------------ |
| 40 | $ git checkout -b <branch> --track <remote>/<branch> |
| 41 | ------------ |
| 42 | + |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 43 | You could omit `<branch>`, in which case the command degenerates to |
Evan Zacks | be94568 | 2017-09-12 15:58:39 -0700 | [diff] [blame] | 44 | "check out the current branch", which is a glorified no-op with |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 45 | rather expensive side-effects to show only the tracking information, |
Elijah Newren | 8936352 | 2023-10-08 06:45:07 +0000 | [diff] [blame] | 46 | if it exists, for the current branch. |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 47 | |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 48 | 'git checkout' -b|-B <new-branch> [<start-point>]:: |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 49 | |
| 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 Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 56 | + |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 57 | If `-B` is given, `<new-branch>` is created if it doesn't exist; otherwise, it |
Tay Ray Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 58 | is reset. This is the transactional equivalent of |
| 59 | + |
| 60 | ------------ |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 61 | $ git branch -f <branch> [<start-point>] |
Tay Ray Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 62 | $ git checkout <branch> |
| 63 | ------------ |
| 64 | + |
| 65 | that is to say, the branch is not reset/created unless "git checkout" is |
Junio C Hamano | b23285a | 2023-11-23 15:00:31 +0900 | [diff] [blame] | 66 | successful (e.g., when the branch is in use in another worktree, not |
| 67 | just the current branch stays the same, but the branch is not reset to |
| 68 | the start-point, either). |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 69 | |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 70 | 'git checkout' --detach [<branch>]:: |
Junio C Hamano | 26776c9 | 2013-09-11 10:05:17 -0700 | [diff] [blame] | 71 | 'git checkout' [--detach] <commit>:: |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 72 | |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 73 | Prepare to work on top of `<commit>`, by detaching `HEAD` at it |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 74 | (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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 80 | When the `<commit>` argument is a branch name, the `--detach` option can |
| 81 | be used to detach `HEAD` at the tip of the branch (`git checkout |
| 82 | <branch>` would check out that branch without detaching `HEAD`). |
Junio C Hamano | 26776c9 | 2013-09-11 10:05:17 -0700 | [diff] [blame] | 83 | + |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 84 | Omitting `<branch>` detaches `HEAD` at the tip of the current branch. |
Chris Rorvick | e1cdf63 | 2012-12-17 00:45:01 -0600 | [diff] [blame] | 85 | |
Alexandr Miloslavskiy | 8ea1189 | 2019-12-03 14:02:16 +0000 | [diff] [blame] | 86 | 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...:: |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 87 | 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]:: |
A Large Angry SCM | 0270f7c | 2005-09-07 17:17:18 -0400 | [diff] [blame] | 88 | |
Alexandr Miloslavskiy | 8ea1189 | 2019-12-03 14:02:16 +0000 | [diff] [blame] | 89 | 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ögershausen | c4ac525 | 2015-06-17 09:54:51 +0200 | [diff] [blame] | 94 | + |
Jonathan Nieder | b831ded | 2010-06-01 02:25:23 -0500 | [diff] [blame] | 95 | The index may contain unmerged entries because of a previous failed merge. |
| 96 | By default, if you try to check out such an entry from the index, the |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 97 | checkout operation will fail and nothing will be checked out. |
Jonathan Nieder | b831ded | 2010-06-01 02:25:23 -0500 | [diff] [blame] | 98 | Using `-f` will ignore these unmerged entries. The contents from a |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 99 | specific side of the merge can be checked out of the index by |
Jonathan Nieder | b831ded | 2010-06-01 02:25:23 -0500 | [diff] [blame] | 100 | using `--ours` or `--theirs`. With `-m`, changes made to the working tree |
| 101 | file can be discarded to re-create the original conflicted merge result. |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 102 | |
Junio C Hamano | b59698a | 2017-10-11 11:21:03 +0900 | [diff] [blame] | 103 | 'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]:: |
Alexandr Miloslavskiy | 6fdc9ad | 2019-12-03 14:02:15 +0000 | [diff] [blame] | 104 | 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 Hamano | b59698a | 2017-10-11 11:21:03 +0900 | [diff] [blame] | 108 | |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 109 | OPTIONS |
| 110 | ------- |
Nicolas Pitre | 6124aee | 2007-02-01 12:31:26 -0500 | [diff] [blame] | 111 | -q:: |
René Scharfe | f7aec12 | 2009-08-29 11:05:00 +0200 | [diff] [blame] | 112 | --quiet:: |
Dave Watson | 2be7fcb | 2007-08-19 22:27:52 -0400 | [diff] [blame] | 113 | Quiet, suppress feedback messages. |
Nicolas Pitre | 6124aee | 2007-02-01 12:31:26 -0500 | [diff] [blame] | 114 | |
Nguyễn Thái Ngọc Duy | d333f67 | 2019-03-29 17:38:53 +0700 | [diff] [blame] | 115 | --progress:: |
| 116 | --no-progress:: |
Edmundo Carmona Antoranz | 870ebdb | 2015-11-01 15:19:05 -0600 | [diff] [blame] | 117 | 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 SCM | 0270f7c | 2005-09-07 17:17:18 -0400 | [diff] [blame] | 122 | -f:: |
René Scharfe | f7aec12 | 2009-08-29 11:05:00 +0200 | [diff] [blame] | 123 | --force:: |
Junio C Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 124 | When switching branches, proceed even if the index or the |
Elijah Newren | 0e29222 | 2021-09-27 16:33:48 +0000 | [diff] [blame] | 125 | 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 Hamano | db94109 | 2008-08-30 07:46:55 -0700 | [diff] [blame] | 128 | + |
| 129 | When checking out paths from the index, do not fail upon unmerged |
| 130 | entries; instead, unmerged entries are ignored. |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 131 | |
Junio C Hamano | 38901a4 | 2008-08-30 07:48:18 -0700 | [diff] [blame] | 132 | --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. Eugster | f303016 | 2015-07-10 13:07:11 -0700 | [diff] [blame] | 136 | + |
| 137 | Note that during `git rebase` and `git pull --rebase`, 'ours' and |
| 138 | 'theirs' may appear swapped; `--ours` gives the version from the |
| 139 | branch the changes are rebased onto, while `--theirs` gives the |
| 140 | version from the branch that holds your work that is being rebased. |
| 141 | + |
| 142 | This is because `rebase` is used in a workflow that treats the |
| 143 | history at the remote as the shared canonical one, and treats the |
| 144 | work done on the branch you are rebasing as the third-party work to |
| 145 | be integrated, and you are temporarily assuming the role of the |
| 146 | keeper of the canonical history during the rebase. As the keeper of |
| 147 | the canonical history, you need to view the history from the remote |
| 148 | as `ours` (i.e. "our shared canonical history"), while what you did |
| 149 | on your side branch as `theirs` (i.e. "one contributor's work on top |
| 150 | of it"). |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 151 | |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 152 | -b <new-branch>:: |
Junio C Hamano | fedb8ea | 2023-01-19 09:12:41 -0800 | [diff] [blame] | 153 | 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 Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 156 | |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 157 | -B <new-branch>:: |
Junio C Hamano | fedb8ea | 2023-01-19 09:12:41 -0800 | [diff] [blame] | 158 | 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 Chuan | 02ac983 | 2010-06-24 03:29:00 +0800 | [diff] [blame] | 163 | |
Stephan Beyer | 3240240 | 2008-06-08 03:36:09 +0200 | [diff] [blame] | 164 | -t:: |
René Scharfe | 6327f0e | 2022-01-20 13:35:54 +0100 | [diff] [blame] | 165 | --track[=(direct|inherit)]:: |
Jeff King | 26d22dc | 2009-04-13 07:18:52 -0400 | [diff] [blame] | 166 | When creating a new branch, set up "upstream" configuration. See |
| 167 | "--track" in linkgit:git-branch[1] for details. |
Johannes Schindelin | bb0ceb6 | 2008-08-09 16:00:12 +0200 | [diff] [blame] | 168 | + |
Matthieu Moy | 23f8239 | 2016-06-28 13:40:10 +0200 | [diff] [blame] | 169 | If no `-b` option is given, the name of the new branch will be |
Johan Herland | fa83a33 | 2013-04-21 23:52:01 +0200 | [diff] [blame] | 170 | derived from the remote-tracking branch, by looking at the local part of |
| 171 | the refspec configured for the corresponding remote, and then stripping |
| 172 | the initial part up to the "*". |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 173 | This would tell us to use `hack` as the local branch when branching |
| 174 | off 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 Riesen | 9188ed8 | 2008-08-21 19:23:20 +0200 | [diff] [blame] | 176 | guessing results in an empty name, the guessing is aborted. You can |
Matthieu Moy | 23f8239 | 2016-06-28 13:40:10 +0200 | [diff] [blame] | 177 | explicitly give a name with `-b` in such a case. |
Paolo Bonzini | 0746d19 | 2007-03-08 10:58:35 +0100 | [diff] [blame] | 178 | |
| 179 | --no-track:: |
Jeff King | 167d744 | 2009-04-13 07:11:56 -0400 | [diff] [blame] | 180 | Do not set up "upstream" configuration, even if the |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 181 | `branch.autoSetupMerge` configuration variable is true. |
Paolo Bonzini | 0746d19 | 2007-03-08 10:58:35 +0100 | [diff] [blame] | 182 | |
Nguyễn Thái Ngọc Duy | ccb111b | 2019-03-29 17:39:13 +0700 | [diff] [blame] | 183 | --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 | + |
| 193 | If the branch exists in multiple remotes and one of them is named by |
| 194 | the `checkout.defaultRemote` configuration variable, we'll use that |
| 195 | one for the purposes of disambiguation, even if the `<branch>` isn't |
| 196 | unique across all remotes. Set it to |
| 197 | e.g. `checkout.defaultRemote=origin` to always checkout remote |
| 198 | branches from there if `<branch>` is ambiguous but exists on the |
| 199 | 'origin' remote. See also `checkout.defaultRemote` in |
| 200 | linkgit:git-config[1]. |
| 201 | + |
Denton Liu | 64f1f58 | 2020-10-07 22:48:15 -0700 | [diff] [blame] | 202 | `--guess` is the default behavior. Use `--no-guess` to disable it. |
| 203 | + |
| 204 | The default behavior can be set via the `checkout.guess` configuration |
| 205 | variable. |
Jon Loeliger | 71bb103 | 2006-03-17 18:26:01 -0600 | [diff] [blame] | 206 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 207 | -l:: |
Jeff King | 26d22dc | 2009-04-13 07:18:52 -0400 | [diff] [blame] | 208 | Create the new branch's reflog; see linkgit:git-branch[1] for |
| 209 | details. |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 210 | |
Eric Sunshine | 07351d9 | 2020-09-06 20:02:20 -0400 | [diff] [blame] | 211 | -d:: |
Junio C Hamano | 3266967 | 2011-02-08 04:32:49 -0600 | [diff] [blame] | 212 | --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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 215 | This is the default behavior of `git checkout <commit>` when |
| 216 | `<commit>` is not a branch name. See the "DETACHED HEAD" section |
Junio C Hamano | 3266967 | 2011-02-08 04:32:49 -0600 | [diff] [blame] | 217 | below for details. |
| 218 | |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 219 | --orphan <new-branch>:: |
Junio C Hamano | 49dc156 | 2023-11-24 12:09:18 +0900 | [diff] [blame] | 220 | Create a new unborn branch, named `<new-branch>`, started from |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 221 | `<start-point>` and switch to it. The first commit made on this |
Erick Mattos | feb98d1 | 2010-05-21 21:28:35 -0300 | [diff] [blame] | 222 | 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 Mattos | 9db5ebf | 2010-03-21 12:34:38 -0300 | [diff] [blame] | 225 | + |
Erick Mattos | feb98d1 | 2010-05-21 21:28:35 -0300 | [diff] [blame] | 226 | The index and the working tree are adjusted as if you had previously run |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 227 | `git checkout <start-point>`. This allows you to start a new history |
| 228 | that records a set of paths similar to `<start-point>` by easily running |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 229 | `git commit -a` to make the root commit. |
Erick Mattos | 9db5ebf | 2010-03-21 12:34:38 -0300 | [diff] [blame] | 230 | + |
Erick Mattos | feb98d1 | 2010-05-21 21:28:35 -0300 | [diff] [blame] | 231 | This can be useful when you want to publish the tree from a commit |
| 232 | without exposing its full history. You might want to do this to publish |
| 233 | an open source branch of a project whose current tree is "clean", but |
| 234 | whose full history contains proprietary or otherwise encumbered bits of |
| 235 | code. |
| 236 | + |
| 237 | If you want to start a disconnected history that records a set of paths |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 238 | that is totally different from the one of `<start-point>`, then you should |
Erick Mattos | feb98d1 | 2010-05-21 21:28:35 -0300 | [diff] [blame] | 239 | clear the index and the working tree right after creating the orphan |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 240 | branch by running `git rm -rf .` from the top level of the working tree. |
Erick Mattos | feb98d1 | 2010-05-21 21:28:35 -0300 | [diff] [blame] | 241 | Afterwards you will be ready to prepare your new files, repopulating the |
| 242 | working tree, by copying them from elsewhere, extracting a tarball, etc. |
Erick Mattos | 9db5ebf | 2010-03-21 12:34:38 -0300 | [diff] [blame] | 243 | |
Nguyễn Thái Ngọc Duy | 08d595d | 2013-04-13 09:12:08 +1000 | [diff] [blame] | 244 | --ignore-skip-worktree-bits:: |
| 245 | In sparse checkout mode, `git checkout -- <paths>` would |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 246 | 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 Duy | 08d595d | 2013-04-13 09:12:08 +1000 | [diff] [blame] | 249 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 250 | -m:: |
Junio C Hamano | eac5a40 | 2008-08-31 19:32:40 -0700 | [diff] [blame] | 251 | --merge:: |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 252 | When switching branches, |
| 253 | if you have local modifications to one or more files that |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 254 | 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 | + |
| 261 | When a merge conflict happens, the index entries for conflicting |
| 262 | paths are left unmerged, and you need to resolve the conflicts |
Shawn O. Pearce | d7f078b | 2007-02-17 04:43:42 -0500 | [diff] [blame] | 263 | and mark the resolved paths with `git add` (or `git rm` if the merge |
| 264 | should result in deletion of the path). |
Junio C Hamano | 0cf8581 | 2008-08-30 07:52:24 -0700 | [diff] [blame] | 265 | + |
| 266 | When checking out paths from the index, this option lets you recreate |
Junio C Hamano | 54f98fe | 2023-07-31 15:44:07 -0700 | [diff] [blame] | 267 | the conflicted merge in the specified paths. This option cannot be |
| 268 | used when checking out paths from a tree-ish. |
Nguyễn Thái Ngọc Duy | a7256de | 2019-03-19 16:39:10 +0700 | [diff] [blame] | 269 | + |
| 270 | When switching branches with `--merge`, staged changes may be lost. |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 271 | |
Junio C Hamano | eac5a40 | 2008-08-31 19:32:40 -0700 | [diff] [blame] | 272 | --conflict=<style>:: |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 273 | The same as `--merge` option above, but changes the way the |
Junio C Hamano | eac5a40 | 2008-08-31 19:32:40 -0700 | [diff] [blame] | 274 | conflicting hunks are presented, overriding the |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 275 | `merge.conflictStyle` configuration variable. Possible values are |
Elijah Newren | ddfc44a8 | 2021-12-01 00:05:07 +0000 | [diff] [blame] | 276 | "merge" (default), "diff3", and "zdiff3". |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 277 | |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 278 | -p:: |
| 279 | --patch:: |
| 280 | Interactively select hunks in the difference between the |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 281 | `<tree-ish>` (or the index, if unspecified) and the working |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 282 | tree. The chosen hunks are then applied in reverse to the |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 283 | working tree (and if a `<tree-ish>` was specified, the index). |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 284 | + |
| 285 | This means that you can use `git checkout -p` to selectively discard |
Valentin Haenel | a31538e | 2011-05-05 20:48:48 +0200 | [diff] [blame] | 286 | edits from your current working tree. See the ``Interactive Mode'' |
Jeff King | 6cf378f | 2012-04-26 04:51:57 -0400 | [diff] [blame] | 287 | section of linkgit:git-add[1] to learn how to operate the `--patch` mode. |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 288 | + |
| 289 | Note that this option uses the no overlay mode by default (see also |
Nguyễn Thái Ngọc Duy | d333f67 | 2019-03-29 17:38:53 +0700 | [diff] [blame] | 290 | `--overlay`), and currently doesn't support overlay mode. |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 291 | |
Nguyễn Thái Ngọc Duy | 1d0fa89 | 2015-01-03 16:41:26 +0700 | [diff] [blame] | 292 | --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 Duy | 9d223d4 | 2019-03-29 17:38:55 +0700 | [diff] [blame] | 298 | --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 Duy | d333f67 | 2019-03-29 17:38:53 +0700 | [diff] [blame] | 304 | --recurse-submodules:: |
| 305 | --no-recurse-submodules:: |
Damien Robert | acbfae3 | 2020-04-06 15:57:09 +0200 | [diff] [blame] | 306 | Using `--recurse-submodules` will update the content of all active |
Stefan Beller | 1fc458d | 2017-03-14 14:46:41 -0700 | [diff] [blame] | 307 | 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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 309 | will fail unless `-f` is used. If nothing (or `--no-recurse-submodules`) |
Damien Robert | b3cec57 | 2020-04-06 15:57:06 +0200 | [diff] [blame] | 310 | is used, submodules working trees will not be updated. |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 311 | Just like linkgit:git-submodule[1], this will detach `HEAD` of the |
| 312 | submodule. |
Stefan Beller | 1fc458d | 2017-03-14 14:46:41 -0700 | [diff] [blame] | 313 | |
Nguyễn Thái Ngọc Duy | d333f67 | 2019-03-29 17:38:53 +0700 | [diff] [blame] | 314 | --overlay:: |
| 315 | --no-overlay:: |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 316 | 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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 319 | working tree, but not in `<tree-ish>` are removed, to make them |
| 320 | match `<tree-ish>` exactly. |
Thomas Gummerer | 091e04b | 2019-01-08 21:52:24 +0000 | [diff] [blame] | 321 | |
Alexandr Miloslavskiy | a9aecc7 | 2019-12-03 14:02:18 +0000 | [diff] [blame] | 322 | --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 SCM | 0270f7c | 2005-09-07 17:17:18 -0400 | [diff] [blame] | 335 | <branch>:: |
Jeff King | 0808723 | 2009-04-13 07:21:04 -0400 | [diff] [blame] | 336 | 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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 339 | commit, your `HEAD` becomes "detached" and you are no longer on |
Jeff King | 0808723 | 2009-04-13 07:21:04 -0400 | [diff] [blame] | 340 | any branch (see below for details). |
Thomas Rast | 696acf4 | 2009-01-17 17:09:56 +0100 | [diff] [blame] | 341 | + |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 342 | You can use the `@{-N}` syntax to refer to the N-th last |
Kaartic Sivaraam | 75ce149 | 2017-11-27 22:58:33 +0530 | [diff] [blame] | 343 | branch/commit checked out using "git checkout" operation. You may |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 344 | also specify `-` which is synonymous to `@{-1}`. |
Michael J Gruber | 873c347 | 2010-06-01 17:16:42 +0200 | [diff] [blame] | 345 | + |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 346 | As a special case, you may use `A...B` as a shortcut for the |
Michael J Gruber | 873c347 | 2010-06-01 17:16:42 +0200 | [diff] [blame] | 347 | merge base of `A` and `B` if there is exactly one merge base. You can |
| 348 | leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. |
Junio C Hamano | 5e1a2e8 | 2007-01-17 10:43:50 -0800 | [diff] [blame] | 349 | |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 350 | <new-branch>:: |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 351 | Name for the new branch. |
| 352 | |
Jean-Noël Avila | 133db54 | 2021-11-06 19:48:52 +0100 | [diff] [blame] | 353 | <start-point>:: |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 354 | The name of a commit at which to start the new branch; see |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 355 | linkgit:git-branch[1] for details. Defaults to `HEAD`. |
Denton Liu | e3d6539 | 2019-04-27 05:02:22 -0700 | [diff] [blame] | 356 | + |
| 357 | As a special case, you may use `"A...B"` as a shortcut for the |
| 358 | merge base of `A` and `B` if there is exactly one merge base. You can |
| 359 | leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 360 | |
| 361 | <tree-ish>:: |
| 362 | Tree to checkout from (when paths are given). If not specified, |
| 363 | the index will be used. |
Denton Liu | c693ef7 | 2020-10-07 00:56:16 -0700 | [diff] [blame] | 364 | + |
| 365 | As a special case, you may use `"A...B"` as a shortcut for the |
| 366 | merge base of `A` and `B` if there is exactly one merge base. You can |
| 367 | leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 368 | |
Alexandr Miloslavskiy | 8ea1189 | 2019-12-03 14:02:16 +0000 | [diff] [blame] | 369 | \--:: |
| 370 | Do not interpret any more arguments as options. |
Jeff King | 76cfadf | 2009-04-13 07:19:33 -0400 | [diff] [blame] | 371 | |
Alexandr Miloslavskiy | 8ea1189 | 2019-12-03 14:02:16 +0000 | [diff] [blame] | 372 | <pathspec>...:: |
| 373 | Limits the paths affected by the operation. |
| 374 | + |
| 375 | For more details, see the 'pathspec' entry in linkgit:gitglossary[7]. |
Junio C Hamano | 5e1a2e8 | 2007-01-17 10:43:50 -0800 | [diff] [blame] | 376 | |
Junio C Hamano | 3266967 | 2011-02-08 04:32:49 -0600 | [diff] [blame] | 377 | DETACHED HEAD |
Junio C Hamano | 5e1a2e8 | 2007-01-17 10:43:50 -0800 | [diff] [blame] | 378 | ------------- |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 379 | `HEAD` normally refers to a named branch (e.g. `master`). Meanwhile, each |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 380 | branch refers to a specific commit. Let's look at a repo with three |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 381 | commits, one of them tagged, and with branch `master` checked out: |
Junio C Hamano | 5e1a2e8 | 2007-01-17 10:43:50 -0800 | [diff] [blame] | 382 | |
| 383 | ------------ |
Andreas Heiduk | 39a3682 | 2018-10-22 22:45:42 +0200 | [diff] [blame] | 384 | HEAD (refers to branch 'master') |
| 385 | | |
| 386 | v |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 387 | a---b---c branch 'master' (refers to commit 'c') |
| 388 | ^ |
| 389 | | |
| 390 | tag 'v2.0' (refers to commit 'b') |
Junio C Hamano | 5e1a2e8 | 2007-01-17 10:43:50 -0800 | [diff] [blame] | 391 | ------------ |
| 392 | |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 393 | When a commit is created in this state, the branch is updated to refer to |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 394 | the new commit. Specifically, 'git commit' creates a new commit `d`, whose |
| 395 | parent is commit `c`, and then updates branch `master` to refer to new |
| 396 | commit `d`. `HEAD` still refers to branch `master` and so indirectly now refers |
| 397 | to commit `d`: |
Junio C Hamano | 5e1a2e8 | 2007-01-17 10:43:50 -0800 | [diff] [blame] | 398 | |
Junio C Hamano | cec8d14 | 2007-02-13 08:58:01 -0800 | [diff] [blame] | 399 | ------------ |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 400 | $ edit; git add; git commit |
| 401 | |
Andreas Heiduk | 39a3682 | 2018-10-22 22:45:42 +0200 | [diff] [blame] | 402 | HEAD (refers to branch 'master') |
| 403 | | |
| 404 | v |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 405 | a---b---c---d branch 'master' (refers to commit 'd') |
| 406 | ^ |
| 407 | | |
| 408 | tag 'v2.0' (refers to commit 'b') |
| 409 | ------------ |
| 410 | |
| 411 | It is sometimes useful to be able to checkout a commit that is not at |
| 412 | the tip of any named branch, or even to create a new commit that is not |
| 413 | referenced by a named branch. Let's look at what happens when we |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 414 | checkout commit `b` (here we show two ways this may be done): |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 415 | |
| 416 | ------------ |
| 417 | $ git checkout v2.0 # or |
| 418 | $ git checkout master^^ |
| 419 | |
| 420 | HEAD (refers to commit 'b') |
| 421 | | |
| 422 | v |
| 423 | a---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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 429 | Notice that regardless of which checkout command we use, `HEAD` now refers |
| 430 | directly to commit `b`. This is known as being in detached `HEAD` state. |
| 431 | It means simply that `HEAD` refers to a specific commit, as opposed to |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 432 | referring 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 | / |
| 442 | a---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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 448 | There is now a new commit `e`, but it is referenced only by `HEAD`. We can |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 449 | of 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 | / |
| 459 | a---b---c---d branch 'master' (refers to commit 'd') |
| 460 | ^ |
| 461 | | |
| 462 | tag 'v2.0' (refers to commit 'b') |
| 463 | ------------ |
| 464 | |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 465 | In fact, we can perform all the normal Git operations. But, let's look |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 466 | at what happens when we then checkout `master`: |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 467 | |
| 468 | ------------ |
| 469 | $ git checkout master |
| 470 | |
Andreas Heiduk | 39a3682 | 2018-10-22 22:45:42 +0200 | [diff] [blame] | 471 | HEAD (refers to branch 'master') |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 472 | e---f | |
| 473 | / v |
| 474 | a---b---c---d branch 'master' (refers to commit 'd') |
| 475 | ^ |
| 476 | | |
| 477 | tag 'v2.0' (refers to commit 'b') |
| 478 | ------------ |
| 479 | |
| 480 | It is important to realize that at this point nothing refers to commit |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 481 | `f`. Eventually commit `f` (and by extension commit `e`) will be deleted |
Thomas Ackermann | 2de9b71 | 2013-01-21 20:17:53 +0100 | [diff] [blame] | 482 | by the routine Git garbage collection process, unless we create a reference |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 483 | before that happens. If we have not yet moved away from commit `f`, |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 484 | any of these will create a reference to it: |
| 485 | |
| 486 | ------------ |
Yutaro Ohno | 9e37969 | 2023-01-09 10:47:17 +0000 | [diff] [blame] | 487 | $ git checkout -b foo # or "git switch -c foo" <1> |
| 488 | $ git branch foo <2> |
| 489 | $ git tag foo <3> |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 490 | ------------ |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 491 | <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 Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 494 | <2> similarly creates a new branch `foo`, which refers to commit `f`, |
| 495 | but leaves `HEAD` detached. |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 496 | <3> creates a new tag `foo`, which refers to commit `f`, |
| 497 | leaving `HEAD` detached. |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 498 | |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 499 | If we have moved away from commit `f`, then we must first recover its object |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 500 | name (typically by using git reflog), and then we can create a reference to |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 501 | it. For example, to see the last two commits to which `HEAD` referred, we |
Jay Soffian | be8ef33 | 2011-02-20 00:21:50 -0500 | [diff] [blame] | 502 | can use either of these commands: |
| 503 | |
| 504 | ------------ |
| 505 | $ git reflog -2 HEAD # or |
Junio C Hamano | cec8d14 | 2007-02-13 08:58:01 -0800 | [diff] [blame] | 506 | $ git log -g -2 HEAD |
| 507 | ------------ |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 508 | |
Nguyễn Thái Ngọc Duy | 19e5656 | 2016-09-07 18:19:40 +0700 | [diff] [blame] | 509 | ARGUMENT DISAMBIGUATION |
| 510 | ----------------------- |
| 511 | |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 512 | When there is only one argument given and it is not `--` (e.g. `git |
| 513 | checkout 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 Duy | 19e5656 | 2016-09-07 18:19:40 +0700 | [diff] [blame] | 515 | or a directory whose name is "abc" exists), Git would usually ask |
| 516 | you to disambiguate. Because checking out a branch is so common an |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 517 | operation, however, `git checkout abc` takes "abc" as a `<tree-ish>` |
Nguyễn Thái Ngọc Duy | 19e5656 | 2016-09-07 18:19:40 +0700 | [diff] [blame] | 518 | in such a situation. Use `git checkout -- <pathspec>` if you want |
| 519 | to checkout these paths out of the index. |
| 520 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 521 | EXAMPLES |
| 522 | -------- |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 523 | |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 524 | === 1. Paths |
| 525 | |
| 526 | The following sequence checks out the `master` branch, reverts |
| 527 | the `Makefile` to two revisions back, deletes `hello.c` by |
| 528 | mistake, and gets it back from the index. |
| 529 | |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 530 | ------------ |
Sean Estabrooks | 48aeecd | 2006-04-28 09:15:05 -0400 | [diff] [blame] | 531 | $ git checkout master <1> |
| 532 | $ git checkout master~2 Makefile <2> |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 533 | $ rm -f hello.c |
Sean Estabrooks | 48aeecd | 2006-04-28 09:15:05 -0400 | [diff] [blame] | 534 | $ git checkout hello.c <3> |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 535 | ------------ |
Sean Estabrooks | 48aeecd | 2006-04-28 09:15:05 -0400 | [diff] [blame] | 536 | <1> switch branch |
Michael J Gruber | c7cb12b | 2009-03-10 16:06:30 +0100 | [diff] [blame] | 537 | <2> take a file out of another commit |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 538 | <3> restore `hello.c` from the index |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 539 | |
Junio C Hamano | caae319 | 2012-09-04 08:28:27 -0700 | [diff] [blame] | 540 | If you want to check out _all_ C source files out of the index, |
| 541 | you can say |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 542 | |
Junio C Hamano | caae319 | 2012-09-04 08:28:27 -0700 | [diff] [blame] | 543 | ------------ |
| 544 | $ git checkout -- '*.c' |
| 545 | ------------ |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 546 | |
Junio C Hamano | caae319 | 2012-09-04 08:28:27 -0700 | [diff] [blame] | 547 | Note the quotes around `*.c`. The file `hello.c` will also be |
| 548 | checked out, even though it is no longer in the working tree, |
| 549 | because the file globbing is used to match entries in the index |
| 550 | (not in the working tree by the shell). |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 551 | |
Sean Estabrooks | 48aeecd | 2006-04-28 09:15:05 -0400 | [diff] [blame] | 552 | If you have an unfortunate branch that is named `hello.c`, this |
| 553 | step would be confused as an instruction to switch to that branch. |
| 554 | You should instead write: |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 555 | |
Junio C Hamano | 4aaa702 | 2005-10-18 01:29:27 -0700 | [diff] [blame] | 556 | ------------ |
| 557 | $ git checkout -- hello.c |
| 558 | ------------ |
| 559 | |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 560 | === 2. Merge |
| 561 | |
| 562 | After working in the wrong branch, switching to the correct |
| 563 | branch would be done using: |
| 564 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 565 | ------------ |
| 566 | $ git checkout mytopic |
| 567 | ------------ |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 568 | |
Nguyễn Thái Ngọc Duy | 181e372 | 2019-03-29 17:38:56 +0700 | [diff] [blame] | 569 | However, your "wrong" branch and correct `mytopic` branch may |
Michael J Gruber | c7cb12b | 2009-03-10 16:06:30 +0100 | [diff] [blame] | 570 | differ in files that you have modified locally, in which case |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 571 | the above checkout would fail like this: |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 572 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 573 | ------------ |
| 574 | $ git checkout mytopic |
Nicolas Sebrecht | 142183d | 2010-07-09 22:27:48 +0200 | [diff] [blame] | 575 | error: You have local changes to 'frotz'; not switching branches. |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 576 | ------------ |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 577 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 578 | You can give the `-m` flag to the command, which would try a |
| 579 | three-way merge: |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 580 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 581 | ------------ |
| 582 | $ git checkout -m mytopic |
| 583 | Auto-merging frotz |
| 584 | ------------ |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 585 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 586 | After this three-way merge, the local modifications are _not_ |
| 587 | registered in your index file, so `git diff` would show you what |
| 588 | changes you made since the tip of the new branch. |
| 589 | |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 590 | === 3. Merge conflict |
| 591 | |
| 592 | When a merge conflict happens during switching branches with |
| 593 | the `-m` option, you would see something like this: |
| 594 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 595 | ------------ |
| 596 | $ git checkout -m mytopic |
| 597 | Auto-merging frotz |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 598 | ERROR: Merge conflict in frotz |
| 599 | fatal: merge program failed |
| 600 | ------------ |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 601 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 602 | At this point, `git diff` shows the changes cleanly merged as in |
| 603 | the previous example, as well as the changes in the conflicted |
| 604 | files. Edit and resolve the conflict and mark it resolved with |
Shawn O. Pearce | d7f078b | 2007-02-17 04:43:42 -0500 | [diff] [blame] | 605 | `git add` as usual: |
Felipe Contreras | 8dda6c3 | 2023-04-18 01:00:48 -0600 | [diff] [blame] | 606 | |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 607 | ------------ |
| 608 | $ edit frotz |
Shawn O. Pearce | d7f078b | 2007-02-17 04:43:42 -0500 | [diff] [blame] | 609 | $ git add frotz |
Junio C Hamano | 1be0659 | 2006-01-12 14:04:36 -0800 | [diff] [blame] | 610 | ------------ |
| 611 | |
Ævar Arnfjörð Bjarmason | 9274dea | 2022-09-07 10:27:05 +0200 | [diff] [blame] | 612 | CONFIGURATION |
| 613 | ------------- |
| 614 | |
| 615 | include::includes/cmd-config-section-all.txt[] |
| 616 | |
| 617 | include::config/checkout.txt[] |
| 618 | |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 619 | SEE ALSO |
| 620 | -------- |
Nguyễn Thái Ngọc Duy | 46e91b6 | 2019-04-25 16:45:45 +0700 | [diff] [blame] | 621 | linkgit:git-switch[1], |
| 622 | linkgit:git-restore[1] |
Nguyễn Thái Ngọc Duy | d787d31 | 2019-03-29 17:39:05 +0700 | [diff] [blame] | 623 | |
Junio C Hamano | 7fc9d69 | 2005-08-23 01:49:47 -0700 | [diff] [blame] | 624 | GIT |
| 625 | --- |
Christian Couder | 9e1f0a8 | 2008-06-06 09:07:32 +0200 | [diff] [blame] | 626 | Part of the linkgit:git[1] suite |