Git: วิธีการกลับมาจากสถานะ 'HEAD HEAD'


218

ถ้าใครจะชำระเงินสาขา:

git checkout 760ac7e

จากตัวอย่างเช่นb9ac70bเราจะกลับไปยังหัวสุดท้ายที่รู้จักb9ac70bโดยไม่รู้จัก SHA1 ได้อย่างไร

คำตอบ:


332

ถ้าคุณจำได้ว่าสาขาไหนเช็คเอาต์ก่อน (เช่นmaster) คุณสามารถทำได้ง่ายๆ

git checkout master

เพื่อออกจากสถานะHEAD ที่แยกออก

โดยทั่วไปการพูด: git checkout <branchname>คุณจะได้รับสิ่งนั้น

หากคุณจำชื่อสาขาล่าสุดไม่ได้ให้ลอง

git checkout -

นอกจากนี้ยังพยายามตรวจสอบสาขาที่คุณเช็คเอาต์ล่าสุด


17
git checkout -- คุณสมบัตินักฆ่า!
dimpiax

1
หากคุณไม่ทำgit checkout -b new_branch_nameคุณเสียความมุ่งมั่นในขณะที่อยู่ในสถานะ HEAD ที่แยกออกหรือไม่
jocassid

2
@ โจคาสซิดใช่คุณทำ มีอยู่ครู่หนึ่ง แต่เมื่อgit gcถูกเรียกใช้จะถูกลบตลอดไป คุณสามารถดูได้git reflogตราบใดที่ยังอยู่ที่นั่น
eckes

คุณจะไม่สูญเสียการผูกมัดหรือการเปลี่ยนแปลงใด ๆ ที่คุณทำไว้ใน HEAD เดี่ยว ๆ ถ้าคุณทำเช่นนี้? นี่ไม่ใช่วิธีที่ดีกว่าใช่ไหม stackoverflow.com/a/61489179/13087176
boopenstein มึนเมา

@tipsyboopenstein ถูกต้อง jocassid พูดถึงสิ่งนี้แล้ว: stackoverflow.com/questions/11801071/ …
eckes

16

ใช้git reflogเพื่อค้นหาแฮชของการกระทำก่อนหน้า

คำสั่งช็อตคัตเพื่อไปยังสาขาที่เช็คเอาต์ล่าสุดของคุณ (ไม่แน่ใจว่าทำงานได้อย่างถูกต้องกับ HEAD ที่แยกออกมาและการคอมมิชชันระดับกลางแม้ว่า) git checkout -


4

ฉันมีกรณีขอบนี้ที่ฉันตรวจสอบรหัสรุ่นก่อนหน้าซึ่งโครงสร้างไดเรกทอรีไฟล์ของฉันแตกต่างกัน:

git checkout 1.87.1                                    
warning: unable to unlink web/sites/default/default.settings.php: Permission denied
... other warnings ...
Note: checking out '1.87.1'.

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 performing another checkout.

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

  git checkout -b <new-branch-name>

HEAD is now at 50a7153d7... Merge branch 'hotfix/1.87.1'

ในกรณีเช่นนี้คุณอาจต้องใช้ - บังคับ (เมื่อคุณรู้ว่าการกลับไปที่สาขาเดิมและการยกเลิกการเปลี่ยนแปลงเป็นสิ่งที่ปลอดภัยที่ต้องทำ)

git checkout master ไม่ทำงาน:

$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
web/sites/default/default.settings.php
... other files ...

git checkout master --force(หรือgit checkout master -f) ทำงาน:

git checkout master -f
Previous HEAD position was 50a7153d7... Merge branch 'hotfix/1.87.1'
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

1

คุณอาจทำคอมมิทใหม่ในdetached HEADรัฐ ฉันเชื่อว่าถ้าคุณทำตามคำตอบอื่น ๆ แนะนำ:

git checkout master
# or
git checkout -

จากนั้นคุณอาจสูญเสียความมุ่งมั่นของคุณ !! คุณอาจต้องการทำสิ่งนี้แทน:

# you are currently in detached HEAD state
git checkout -b commits-from-detached-head

จากนั้นรวมcommits-from-detached-headเข้ากับสาขาใดก็ได้ที่คุณต้องการดังนั้นคุณจะไม่เสียความมุ่งมั่น

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.