3 test_description='checkout into detached HEAD state'
7 test_must_fail git symbolic-ref -q HEAD >/dev/null
10 check_not_detached () {
11 git symbolic-ref -q HEAD >/dev/null
14 ORPHAN_WARNING='you are leaving .* commit.*behind'
15 check_orphan_warning() {
16 test_i18ngrep "$ORPHAN_WARNING" "$1"
18 check_no_orphan_warning() {
19 test_i18ngrep ! "$ORPHAN_WARNING" "$1"
23 git checkout master &&
27 test_expect_success 'setup' '
30 test_commit three && git tag -d three &&
31 test_commit four && git tag -d four &&
36 test_expect_success 'checkout branch does not detach' '
38 git checkout branch &&
42 test_expect_success 'checkout tag detaches' '
48 test_expect_success 'checkout branch by full name detaches' '
50 git checkout refs/heads/branch &&
54 test_expect_success 'checkout non-ref detaches' '
56 git checkout branch^ &&
60 test_expect_success 'checkout ref^0 detaches' '
62 git checkout branch^0 &&
66 test_expect_success 'checkout --detach detaches' '
68 git checkout --detach branch &&
72 test_expect_success 'checkout --detach without branch name' '
74 git checkout --detach &&
78 test_expect_success 'checkout --detach errors out for non-commit' '
80 test_must_fail git checkout --detach one^{tree} &&
84 test_expect_success 'checkout --detach errors out for extra argument' '
86 git checkout master &&
87 test_must_fail git checkout --detach tag one.t &&
91 test_expect_success 'checkout --detached and -b are incompatible' '
93 test_must_fail git checkout --detach -b newbranch tag &&
97 test_expect_success 'checkout --detach moves HEAD' '
100 git checkout --detach two &&
101 git diff --exit-code HEAD &&
102 git diff --exit-code two
105 test_expect_success 'checkout warns on orphan commits' '
107 git checkout --detach two &&
108 echo content >orphan &&
110 git commit -a -m orphan &&
111 git checkout master 2>stderr
114 test_expect_success 'checkout warns on orphan commits: output' '
115 check_orphan_warning stderr
118 test_expect_success 'checkout does not warn leaving ref tip' '
120 git checkout --detach two &&
121 git checkout master 2>stderr
124 test_expect_success 'checkout does not warn leaving ref tip' '
125 check_no_orphan_warning stderr
128 test_expect_success 'checkout does not warn leaving reachable commit' '
130 git checkout --detach HEAD^ &&
131 git checkout master 2>stderr
134 test_expect_success 'checkout does not warn leaving reachable commit' '
135 check_no_orphan_warning stderr
139 Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
141 test_expect_success 'tracking count is accurate after orphan check' '
143 git branch child master^ &&
144 git config branch.child.remote . &&
145 git config branch.child.merge refs/heads/master &&
146 git checkout child^ &&
147 git checkout child >stdout &&
148 test_cmp expect stdout