3 test_description
='checkout into detached HEAD state'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK
=true
11 test_must_fail git symbolic-ref
-q HEAD
>/dev
/null
14 check_not_detached
() {
15 git symbolic-ref
-q HEAD
>/dev
/null
18 PREV_HEAD_DESC
='Previous HEAD position was'
19 check_orphan_warning
() {
20 test_i18ngrep
"you are leaving $2 behind" "$1" &&
21 test_i18ngrep
! "$PREV_HEAD_DESC" "$1"
23 check_no_orphan_warning
() {
24 test_i18ngrep
! "you are leaving .* commit.*behind" "$1" &&
25 test_i18ngrep
"$PREV_HEAD_DESC" "$1"
33 test_expect_success
'setup' '
36 test_commit three && git tag -d three &&
37 test_commit four && git tag -d four &&
42 test_expect_success
'checkout branch does not detach' '
44 git checkout branch &&
48 test_expect_success
'checkout tag detaches' '
54 test_expect_success
'checkout branch by full name detaches' '
56 git checkout refs/heads/branch &&
60 test_expect_success
'checkout non-ref detaches' '
62 git checkout branch^ &&
66 test_expect_success
'checkout ref^0 detaches' '
68 git checkout branch^0 &&
72 test_expect_success
'checkout --detach detaches' '
74 git checkout --detach branch &&
78 test_expect_success
'checkout --detach without branch name' '
80 git checkout --detach &&
84 test_expect_success
'checkout --detach errors out for non-commit' '
86 test_must_fail git checkout --detach one^{tree} &&
90 test_expect_success
'checkout --detach errors out for extra argument' '
93 test_must_fail git checkout --detach tag one.t &&
97 test_expect_success
'checkout --detached and -b are incompatible' '
99 test_must_fail git checkout --detach -b newbranch tag &&
103 test_expect_success
'checkout --detach moves HEAD' '
106 git checkout --detach two &&
107 git diff --exit-code HEAD &&
108 git diff --exit-code two
111 test_expect_success
'checkout warns on orphan commits' '
113 git checkout --detach two &&
114 echo content >orphan &&
116 git commit -a -m orphan1 &&
117 echo new content >orphan &&
118 git commit -a -m orphan2 &&
119 orphan2=$(git rev-parse HEAD) &&
120 git checkout main 2>stderr
123 test_expect_success
'checkout warns on orphan commits: output' '
124 check_orphan_warning stderr "2 commits"
127 test_expect_success
'checkout warns orphaning 1 of 2 commits' '
128 git checkout "$orphan2" &&
129 git checkout HEAD^ 2>stderr
132 test_expect_success
'checkout warns orphaning 1 of 2 commits: output' '
133 check_orphan_warning stderr "1 commit"
136 test_expect_success
'checkout does not warn leaving ref tip' '
138 git checkout --detach two &&
139 git checkout main 2>stderr
142 test_expect_success
'checkout does not warn leaving ref tip' '
143 check_no_orphan_warning stderr
146 test_expect_success
'checkout does not warn leaving reachable commit' '
148 git checkout --detach HEAD^ &&
149 git checkout main 2>stderr
152 test_expect_success
'checkout does not warn leaving reachable commit' '
153 check_no_orphan_warning stderr
157 Your branch is behind 'main' by 1 commit, and can be fast-forwarded.
158 (use "git pull" to update your local branch)
160 test_expect_success
'tracking count is accurate after orphan check' '
162 git branch child main^ &&
163 git config branch.child.remote . &&
164 git config branch.child.merge refs/heads/main &&
165 git checkout child^ &&
166 git checkout child >stdout &&
167 test_cmp expect stdout
170 test_expect_success
'no advice given for explicit detached head state' '
172 test_config advice.detachedHead true &&
173 git checkout child && git checkout HEAD^0 >expect.advice 2>&1 &&
174 test_config advice.detachedHead false &&
175 git checkout child && git checkout HEAD^0 >expect.no-advice 2>&1 &&
176 test_unconfig advice.detachedHead &&
177 # without configuration, the advice.* variables default to true
178 git checkout child && git checkout HEAD^0 >actual 2>&1 &&
179 test_cmp expect.advice actual &&
181 # with explicit --detach
183 test_unconfig advice.detachedHead &&
184 git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
185 test_cmp expect.no-advice actual &&
187 # explicitly decline advice
188 test_config advice.detachedHead false &&
189 git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
190 test_cmp expect.no-advice actual
193 # Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
194 test_expect_success
'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
196 commit=$(git rev-parse --short=12 main^) &&
197 commit2=$(git rev-parse --short=12 main~2) &&
198 commit3=$(git rev-parse --short=12 main~3) &&
200 # The first detach operation is more chatty than the following ones.
201 cat >1st_detach <<-EOF &&
202 Note: switching to 'HEAD^'.
204 You are in 'detached HEAD' state. You can look around, make experimental
205 changes and commit them, and you can discard any commits you make in this
206 state without impacting any branches by switching back to a branch.
208 If you want to create a new branch to retain commits you create, you may
209 do so (now or later) by using -c with the switch command. Example:
211 git switch -c <new-branch-name>
213 Or undo this operation with:
217 Turn off this advice by setting config variable advice.detachedHead to false
219 HEAD is now at \$commit three
222 # The remaining ones just show info about previous and current HEADs.
223 cat >2nd_detach <<-EOF &&
224 Previous HEAD position was \$commit three
225 HEAD is now at \$commit2 two
228 cat >3rd_detach <<-EOF &&
229 Previous HEAD position was \$commit2 two
230 HEAD is now at \$commit3 one
234 check_not_detached &&
236 # Various ways of *not* asking for ellipses
238 sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
239 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
241 test_cmp 1st_detach actual &&
243 GIT_PRINT_SHA1_ELLIPSIS="no
" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
245 test_cmp 2nd_detach actual &&
247 GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
249 test_cmp 3rd_detach actual &&
251 sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
253 # We only have four commits, but we can re-use them
255 check_not_detached &&
257 # Make no mention of the env var at all
258 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
260 test_cmp 1st_detach actual &&
262 GIT_PRINT_SHA1_ELLIPSIS='nope' &&
263 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
265 test_cmp 2nd_detach actual &&
267 GIT_PRINT_SHA1_ELLIPSIS=nein &&
268 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
270 test_cmp 3rd_detach actual &&
275 # Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
276 test_expect_success
'describe_detached_head does print SHA-1 ellipsis when asked to' "
278 commit=$(git rev-parse --short=12 main^) &&
279 commit2=$(git rev-parse --short=12 main~2) &&
280 commit3=$(git rev-parse --short=12 main~3) &&
282 # The first detach operation is more chatty than the following ones.
283 cat >1st_detach <<-EOF &&
284 Note: switching to 'HEAD^'.
286 You are in 'detached HEAD' state. You can look around, make experimental
287 changes and commit them, and you can discard any commits you make in this
288 state without impacting any branches by switching back to a branch.
290 If you want to create a new branch to retain commits you create, you may
291 do so (now or later) by using -c with the switch command. Example:
293 git switch -c <new-branch-name>
295 Or undo this operation with:
299 Turn off this advice by setting config variable advice.detachedHead to false
301 HEAD is now at \$commit... three
304 # The remaining ones just show info about previous and current HEADs.
305 cat >2nd_detach <<-EOF &&
306 Previous HEAD position was \$commit... three
307 HEAD is now at \$commit2... two
310 cat >3rd_detach <<-EOF &&
311 Previous HEAD position was \$commit2... two
312 HEAD is now at \$commit3... one
316 check_not_detached &&
318 # Various ways of asking for ellipses...
319 # The user can just use any kind of quoting (including none).
321 GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
323 test_cmp 1st_detach actual &&
325 GIT_PRINT_SHA1_ELLIPSIS=Yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
327 test_cmp 2nd_detach actual &&
329 GIT_PRINT_SHA1_ELLIPSIS=YES git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
331 test_cmp 3rd_detach actual &&