สิ่งที่ฉันทำในกรณีเหล่านี้คือ:
ในเซิร์ฟเวอร์เลื่อนเคอร์เซอร์กลับไปที่การคอมมิตที่รู้จักล่าสุด:
git push -f origin <last_known_good_commit>:<branch_name>
ทำในพื้นที่เดียวกัน:
git reset --hard <last_known_good_commit>
# ^^^^^^
# optional
ดูตัวอย่างเต็มรูปแบบในสาขาmy_new_branchที่ฉันสร้างขึ้นเพื่อจุดประสงค์นี้:
$ git branch
my_new_branch
นี่คือประวัติล่าสุดหลังจากเพิ่มบางสิ่งลงในmyfile.py:
$ git log
commit 80143bcaaca77963a47c211a9cbe664d5448d546
Author: me
Date: Wed Mar 23 12:48:03 2016 +0100
Adding new stuff in myfile.py
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
ฉันต้องการกำจัดความมุ่งมั่นครั้งล่าสุดซึ่งถูกผลักไปแล้วดังนั้นฉันจึงเรียกใช้:
$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:me/myrepo.git
+ 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)
ดี! ตอนนี้ฉันเห็นไฟล์ที่มีการเปลี่ยนแปลงในการกระทำที่myfile.pyแสดง( ) ใน "ไม่จัดฉากสำหรับการกระทำ":
$ git status
On branch my_new_branch
Your branch is up-to-date with 'origin/my_new_branch'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.py
no changes added to commit (use "git add" and/or "git commit -a")
เนื่องจากฉันไม่ต้องการการเปลี่ยนแปลงเหล่านี้ฉันเพียงแค่เลื่อนเคอร์เซอร์กลับไปข้างบนเช่นกัน:
$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit
ดังนั้นตอนนี้ HEAD อยู่ในการคอมมิชชันก่อนหน้านี้, ทั้งใน local และ remote:
$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit