Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

git - 使用Git将我的最后一个X提交一起压缩(Squash my last X commits together using Git)

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

  ask by markdorison translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this fairly easily without git rebase or git merge --squash .

(您可以很容易地做到这一点,而无需git rebasegit merge --squash 。)

In this example, we'll squash the last 3 commits.

(在此示例中,我们将压缩最后3次提交。)

If you want to write the new commit message from scratch, this suffices:

(如果您想从头开始编写新的提交消息,则满足以下条件:)

git reset --soft HEAD~3 &&
git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages (ie similar to what a pick/squash/squash/…/squash git rebase -i instruction list would start you with), then you need to extract those messages and pass them to git commit :

(如果要开始使用现有提交消息的串联来编辑新的提交消息(即类似于pick / squash / squash /…/ squash git rebase -i指令列表的开头),则需要提取这些消息并将它们传递给git commit :)

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

Both of those methods squash the last three commits into a single new commit in the same way.

(这两种方法都以相同的方式将最后三个提交压缩为单个新提交。)

The soft reset just re-points HEAD to the last commit that you do not want to squash.

(软重置只是将HEAD重新指向您不想挤压的最后一次提交。)

Neither the index nor the working tree are touched by the soft reset, leaving the index in the desired state for your new commit (ie it already has all the changes from the commits that you are about to “throw away”).

(软复位既不会触及索引也不会影响工作树,从而使索引处于您的新提交所需的状态(即,它已经具有您要“丢弃”的提交中的所有更改)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...