選擇應該提交哪些行進行提交
假設你在一個或多個檔案中有許多更改,但是從每個檔案中你只想提交一些更改,你可以使用以下命令選擇所需的更改:
git add -p
要麼
git add -p [file]
你的每項更改都將單獨顯示,每次更改時,系統都會提示你選擇以下選項之一:
y - Yes, add this hunk
n - No, don’t add this hunk
d - No, don’t add this hunk, or any other remaining hunks for this file.
Useful if you’ve already added what you want to, and want to skip over the rest.
s - Split the hunk into smaller hunks, if possible
e - Manually edit the hunk. This is probably the most powerful option.
It will open the hunk in a text editor and you can edit it as needed.
這將暫存你選擇的檔案的各個部分。然後你可以像這樣提交所有階段性的更改:
git commit -m 'Commit Message'
未暫存或提交的更改仍將顯示在你的工作檔案中,如果需要,可以在以後提交。或者如果剩餘的更改是不需要的,可以將它們丟棄:
git reset --hard
除了將較大的更改分解為較小的提交之外,此方法對於檢視你要提交的內容也很有用。通過單獨確認每個更改,你有機會檢查你編寫的內容,並可以避免意外地暫存不需要的程式碼,如 println / logging 語句。