猿问

使用Git一起压缩我的最后一个X

使用Git一起压缩我的最后一个X

如何使用Git将最后一个X提交压缩为一个提交?



绝地无双
浏览 581回答 3
3回答

慕雪6442864

使用git rebase -i <after-this-commit>并将第二次提交和后续提交中的“挑选”替换为“squash”或“Fixup”,如手册.在这个例子中,<after-this-commit>是SHA 1散列或来自当前分支头部的相对位置,从该分支的提交将为rebase命令进行分析。例如,如果用户希望在过去从当前头查看5提交,则命令为git rebase -i HEAD~5.

慕沐林林

你可以很容易地做到这一点。git rebase或git merge --squash。在本例中,我们将压缩最后3次提交。如果您想从头开始编写新的提交消息,这就足够了:git&nbsp;reset&nbsp;--soft&nbsp;HEAD~3&nbsp;&& git&nbsp;commit如果您想开始编辑新的提交消息,并将现有的提交消息连接起来(例如,与Pick/squash/…类似)壁球git rebase -i),然后您需要提取这些消息并将它们传递给git commit:git&nbsp;reset&nbsp;--soft&nbsp;HEAD~3&nbsp;&&&nbsp; git&nbsp;commit&nbsp;--edit&nbsp;-m"$(git&nbsp;log&nbsp;--format=%B&nbsp;--reverse&nbsp;HEAD..HEAD@{1})"这两个方法都以相同的方式将最后三个提交压缩为单个新提交。软重置只是重新指向最后一次提交,你不想压扁。软重置不会触及索引或工作树,使索引处于新提交时所需的状态(也就是说,它已经对即将“丢弃”的提交进行了所有更改)。

翻阅古今

你可以用git merge --squash对于这一点,它比git rebase -i。假设您使用的是主人,并且希望将最后12次提交压缩为一次。警告:首先要确保你的工作-检查一下。git status是干净的(因为git reset --hard将丢弃已上演和未上演的变化)然后:#&nbsp;Reset&nbsp;the&nbsp;current&nbsp;branch&nbsp;to&nbsp;the&nbsp;commit&nbsp;just&nbsp;before&nbsp;the&nbsp;last&nbsp;12: git&nbsp;reset&nbsp;--hard&nbsp;HEAD~12 #&nbsp;HEAD@{1}&nbsp;is&nbsp;where&nbsp;the&nbsp;branch&nbsp;was&nbsp;just&nbsp;before&nbsp;the&nbsp;previous&nbsp;command. #&nbsp;This&nbsp;command&nbsp;sets&nbsp;the&nbsp;state&nbsp;of&nbsp;the&nbsp;index&nbsp;to&nbsp;be&nbsp;as&nbsp;it&nbsp;would&nbsp;just #&nbsp;after&nbsp;a&nbsp;merge&nbsp;from&nbsp;that&nbsp;commit: git&nbsp;merge&nbsp;--squash&nbsp;HEAD@{1} #&nbsp;Commit&nbsp;those&nbsp;squashed&nbsp;changes.&nbsp;&nbsp;The&nbsp;commit&nbsp;message&nbsp;will&nbsp;be&nbsp;helpfully #&nbsp;prepopulated&nbsp;with&nbsp;the&nbsp;commit&nbsp;messages&nbsp;of&nbsp;all&nbsp;the&nbsp;squashed&nbsp;commits: git&nbsp;commit这个文件git merge描述--squash更详细的选项。最新情况:这种方法相对于简单的方法唯一真正的优点是git reset --soft HEAD~12 && git commit由chris johnsen在他的回答是将预填充的提交消息预先填充到正在压缩的每个提交消息中。
随时随地看视频慕课网APP
我要回答