ดำเนินการต่อhttps://stackoverflow.com/a/20574486/4935114 , @Mikeเสนอให้สร้างpre-commithook ซึ่งจะgrepอยู่ในไฟล์ที่จัดฉากสำหรับบรรทัดที่อาจต้องการละเว้น เบ็ดจะตรวจสอบว่าเส้นเหล่านั้นถูกจัดฉากหรือไม่ หากเป็นเช่นนั้นจะมีechoคำเตือนและexitมีรหัส1ดังนั้นกระบวนการคอมมิตจะไม่ดำเนินต่อไป
ได้รับแรงบันดาลใจจากคำตอบของ @ Mikeฉันพบว่าตัวเองใช้ตะขอรุ่นปรับปรุงซึ่งจะใช้ reset (พร้อม-pธง) บรรทัดเฉพาะที่เราต้องการเพิกเฉยโดยอัตโนมัติ
ฉันไม่แน่ใจว่าเบ็ดนี้จะทำงานให้สถานการณ์ที่คุณมีไฟล์จำนวนมากที่มีเส้นนี้ที่จะละเลย แต่เบ็ดจะมองหาการเปลี่ยนแปลงในสายนี้ในแฟ้มที่เฉพาะเจาะจงpre-commit buildVars.javaสคริปต์ hook มีลักษณะเช่นนี้เมื่อฉันทดสอบบนเครื่องของฉัน
#!/bin/sh
# this hook looks for lines with the text `var isPhoneGap = false;` in the file `buildVars.java` and it resets these lines to the previous state before staged with `reset -p`
if [[ $(git diff --no-ext-diff --cached buildVars.java | grep --count -e "var\ isPhoneGap[\ ]*=[\ ]*") -ne 0 ]]; then
cat <<EOW
WARNING: You are attempting to commit changes which are not supposed to be commited according to this \`pre-commit\` hook
This \`pre-commit\` hook will reset all the files containing this line to it's previous state in the last commit.
EOW
echo /$'\n'isPhoneGap$'\n'y$'\n'q | git reset -p
# BONUS: Check if after reseting, there is no actual changes to be commited and if so, exit 1 so the commit process will abort.
if [[ $(git diff --no-ext-diff --cached | wc -l) -eq 0 ]]; then
echo there are no actual changes to be commited and besides the change to the variable \'isPhoneGap\' so I won\'t commit.
exit 1
fi
fi
คำอธิบาย
สิ่งที่ฉันทำคือการสะท้อนลำดับการควบคุมซึ่งค้นหานิพจน์ทั่วไปisPhoneGapในระหว่างresetกระบวนการโต้ตอบ ดังนั้นการลอกเลียนแบบผู้ใช้ที่กด/เพื่อค้นหาisPhoneGap, เครื่องรีดyเมื่อถามว่าเขาต้องการที่จะทิ้งแพทช์นี้และในที่สุดก็กดเพื่อออกจากการโต้ตอบqreset
กระบวนการแพทช์ย้อนกลับแบบโต้ตอบมีการบันทึกไว้ที่นี่: https://git-scm.com/docs/git-add#git-add-patch
หมายเหตุ:สคริปต์ข้างต้นสมมติว่าตัวแปรinteractive.singleKeyคือfalse. หากคุณกำหนดค่าเป็นของคุณให้trueลบคำสั่งใด ๆ ออก$'\n'จากechoคำสั่งทันทีหลังคำเตือน