簡體   English   中英

如何通過git rebase --interactive編輯提交時保留提交消息?

[英]How do I keep the commit message when editing commits via git rebase --interactive?

我目前正處於git rebase --interactive會話中,我正在編輯提交。 我正在按照建議如何拆分歷史上埋藏的Git提交? 即我運行git reset HEAD^並做了我的修改。 現在我想要rebase繼續,這需要我提交我的更改。 我想修改我的舊提交消息,但問題是,如果我運行git commit --amend ,我會我實際修改之前給出提交的提交消息 - 我當然不會我想將我的更改合並到該提交中。

那么如何為我正在進行的提交檢索舊的提交消息呢?

雖然CharlesB的解決方案是正確的並且可能更容易 ,但原始海報他想要編輯的提交之前看到提交消息的原因是因為他正在使用git commit--amend標志,這會修改先前的提交。

而不是使用--amend來提交更改,只需使用沒有標志的git commit ,它不會觸及先前的提交。 您還可以傳入一個選項,以重用使用git reset head^重置的提交中的提交消息:

git commit --reuse-message=HEAD@{1}

# Or use -C, which is the same thing, but shorter:
git commit -C HEAD@{1}

HEAD@{1}指向您執行git reset head^之前的提交。 您也可以直接傳入該提交的sha id。

git commit docs

 -C <commit> --reuse-message=<commit> 

獲取現有提交對象,並在創建提交時重用日志消息和作者信息(包括時間戳)。

當然,就像我說的那樣,CharlesB的解決方案更簡單,因為如果你不做第一個git reset head^ ,你可以直接進行更改並修改你想要修改的提交,並自動獲得之前的提交當你執行git commit --amend ,你不必為它傳遞commit sha。

如果你不想要它,為什么要遵循提交拆分的說明?

您沒有重置為HEAD^ ,這僅適用於拆分提交。 只需在rebase -i標記要編輯的提交,進行更改, commit --amendrebase --continue

我有另一個答案似乎更加萬無一失,盡管沒有證件。

$ git reset HEAD^
$ git add ...               # stage the first part of your split
$ git commit -m 'part one'
$ git commit -a -F $(git rev-parse --git-dir)/rebase-merge/message

我在git-commit(1)git-reset(1)中改編了一些例子:

$ git reset HEAD^
$ git add ...                 # stage the first part of your split
$ git commit -m 'part one'
$ git commit -a -c ORIG_HEAD  # start with the earlier commit message

如果您擔心做一些可能會改變ORIG_HEAD事情,您可以改為:

$ SAVED=$(git rev-parse HEAD)
$ git reset HEAD^
...
$ git commit -a -c $SAVED   # start with the earlier commit message

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM