Git リポジトリが分離 HEAD 状態になったのはなぜですか? 質問する

Git リポジトリが分離 HEAD 状態になったのはなぜですか? 質問する

今日、ヘッドが外れてしまいました。これは、次の記事で説明されているのと同じ問題です。git push はローカルに変更があってもすべてが最新であると表示します

私の知る限り、特別なことは何もしていません。ローカル リポジトリからコミットしてプッシュしただけです。

それで、私はどのようにして を手に入れたのでしょうかdetached HEAD?

ベストアンサー1

いずれかのブランチの名前ではないコミットをチェックアウトすると、分離された HEAD が取得されます。ブランチの先端を表す SHA1 は、分離された HEAD を提供します。ローカル ブランチのチェックアウトのみがこのモードを回避します。

見る分離したHEADでコミットする

HEAD が切り離されている場合、名前付きブランチが更新されないことを除いて、コミットは通常どおり機能します。(これは匿名ブランチと考えることができます。)

代替テキスト

たとえば、最初に追跡せずに「リモート ブランチ」をチェックアウトすると、HEAD が分離してしまう可能性があります。

見るgit: ヘッドをデタッチせずにブランチを切り替える

意味: git checkout origin/main(またはorigin/master昔は) の結果は次のようになります。

Note: switching to 'origin/main'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at a1b2c3d My commit message

だからこそ、git checkoutもうありませんが、新しいgit switch指示。

を使用するとgit switch、リモート ブランチを「チェックアウト」(切り替え) しようとする同じ試みはすぐに失敗します。

git switch origin/main
fatal: a branch is expected, got remote branch 'origin/main'

さらに追加するにはgit switch:

Git 2.23(2019年8月)では、紛らわしいgit checkoutコマンドもう。

git switchブランチをチェックアウトして、デタッチ HEAD を取得することもできますが、次の例外があります。

  • --detach明確な選択肢がある

HEAD~3新しいブランチを作成せずに、一時的な検査や実験のためにコミットをチェックアウトするには:

git switch --detach HEAD~3
HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
  • リモートトラッキングブランチを誤って切り離すことはできません

見る:

C:\Users\vonc\arepo>git checkout origin/master
Note: switching to 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

新しいgit switchコマンドを使用する場合:

C:\Users\vonc\arepo>git switch origin/master
fatal: a branch is expected, got remote branch 'origin/master'

リモート ブランチを追跡する新しいローカル ブランチを作成する場合:

git switch <branch> 

が見つからないが、一致する名前を持つ<branch>追跡ブランチがリモートに1つだけ存在する場合( と呼ぶ)、<remote>

git switch -c <branch> --track <remote>/<branch>

もう間違いはありません!
不要なヘッドの取り外しはもうありません!

そして、 のgit switch <tag>代わりにgit switch --detach <tag>--detachGit 2.36は、不足しているオプションを思い出すのに役立ちます

おすすめ記事