ใช้git rebase --interactive
เพื่อแก้ไขการคอมมิชชันรันgit reset HEAD~
และจากนั้นgit add -p
เพิ่มคอมมิชชันจากนั้นทำการคอมมิชชันแล้วเพิ่มการเพิ่มเติมและทำการคอมมิตอื่น ๆ หลาย ๆ ครั้งตามที่คุณต้องการ เมื่อเสร็จแล้วให้เรียกใช้git rebase --continue
แล้วคุณจะได้รับการแบ่งทั้งหมดก่อนหน้านี้ในสแต็กของคุณ
สำคัญ : โปรดทราบว่าคุณสามารถเล่นและทำการเปลี่ยนแปลงทั้งหมดที่คุณต้องการและไม่ต้องกังวลกับการสูญเสียการเปลี่ยนแปลงเก่า ๆ เพราะคุณสามารถเรียกใช้git reflog
เพื่อค้นหาจุดในโครงการของคุณที่มีการเปลี่ยนแปลงที่คุณต้องการ (เรียกมันว่าa8c4ab
) git reset a8c4ab
และจากนั้น
ต่อไปนี้เป็นชุดคำสั่งเพื่อแสดงวิธีการทำงาน:
mkdir git-test; cd git-test; git init
ตอนนี้เพิ่มไฟล์ A
vi A
เพิ่มบรรทัดนี้:
one
git commit -am one
จากนั้นเพิ่มบรรทัดนี้ใน A:
two
git commit -am two
จากนั้นเพิ่มบรรทัดนี้ใน A:
three
git commit -am three
ตอนนี้ไฟล์ A มีลักษณะดังนี้:
one
two
three
และgit log
ดูเหมือนว่าของเราดังต่อไปนี้ (ดีฉันใช้git log --pretty=oneline --pretty="%h %cn %cr ---- %s"
bfb8e46 Rose Perrone 4 seconds ago ---- three
2b613bc Rose Perrone 14 seconds ago ---- two
9aac58f Rose Perrone 24 seconds ago ---- one
สมมติว่าเราต้องการแยกการกระทำที่สอง, two
.
git rebase --interactive HEAD~2
นี่จะแสดงข้อความที่มีลักษณะดังนี้:
pick 2b613bc two
pick bfb8e46 three
เปลี่ยนรายการแรกpick
เป็น a e
เพื่อแก้ไขที่ส่งมอบ
git reset HEAD~
git diff
แสดงให้เราเห็นว่าเราแค่ทำตามคำมั่นสัญญาที่เราทำไว้สำหรับการกระทำครั้งที่สอง:
diff --git a/A b/A
index 5626abf..814f4a4 100644
--- a/A
+++ b/A
@@ -1 +1,2 @@
one
+two
ขั้นตอน Let 's ว่าการเปลี่ยนแปลงและเพิ่ม "และคนที่สาม" A
เพื่อบรรทัดในแฟ้มที่
git add .
นี่คือจุดระหว่างการรีบูตแบบโต้ตอบที่เราจะเรียกใช้git rebase --continue
เนื่องจากเราเพียงต้องการกลับไปที่กองการคอมมิชชันของเราเพื่อแก้ไขคอมมิชชันก่อนหน้า แต่ครั้งนี้เราต้องการสร้างความมุ่งมั่นใหม่ git commit -am 'two and a third'
ดังนั้นเราจะใช้ ตอนนี้เราแก้ไขไฟล์และเพิ่มบรรทัดA
two and two thirds
git add .
git commit -am 'two and two thirds'
git rebase --continue
เรามีความขัดแย้งกับความมุ่งมั่นของเราthree
ดังนั้นให้แก้ไข:
เราจะเปลี่ยน
one
<<<<<<< HEAD
two and a third
two and two thirds
=======
two
three
>>>>>>> bfb8e46... three
ถึง
one
two and a third
two and two thirds
three
git add .; git rebase --continue
ตอนนี้git log -p
หน้าตาของเราเป็นแบบนี้:
commit e59ca35bae8360439823d66d459238779e5b4892
Author: Rose Perrone <roseperrone@fake.com>
Date: Sun Jul 7 13:57:00 2013 -0700
three
diff --git a/A b/A
index 5aef867..dd8fb63 100644
--- a/A
+++ b/A
@@ -1,3 +1,4 @@
one
two and a third
two and two thirds
+three
commit 4a283ba9bf83ef664541b467acdd0bb4d770ab8e
Author: Rose Perrone <roseperrone@fake.com>
Date: Sun Jul 7 14:07:07 2013 -0700
two and two thirds
diff --git a/A b/A
index 575010a..5aef867 100644
--- a/A
+++ b/A
@@ -1,2 +1,3 @@
one
two and a third
+two and two thirds
commit 704d323ca1bc7c45ed8b1714d924adcdc83dfa44
Author: Rose Perrone <roseperrone@fake.com>
Date: Sun Jul 7 14:06:40 2013 -0700
two and a third
diff --git a/A b/A
index 5626abf..575010a 100644
--- a/A
+++ b/A
@@ -1 +1,2 @@
one
+two and a third
commit 9aac58f3893488ec643fecab3c85f5a2f481586f
Author: Rose Perrone <roseperrone@fake.com>
Date: Sun Jul 7 13:56:40 2013 -0700
one
diff --git a/A b/A
new file mode 100644
index 0000000..5626abf
--- /dev/null
+++ b/A
@@ -0,0 +1 @@
+one