如何合并最后两个提交? [英] How can I merge the last two commits?

查看:41
本文介绍了如何合并最后两个提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合我的最后两个提交.这是git log的结果:

I want to combine my last two commits. Here is the result of git log:

现在,我想将最后两个提交(它们都放入阅读框)合并为一个提交.我该怎么办?

Now I want to merge those last two commits (which are into read box) into one commit. How can I do that?

推荐答案

有很多方法(使用rebasereset).

There are many ways to do it (using rebase or reset).

使用 git reset 的方法:

The approach using git reset:

  1. 无论采用哪种方法,在进行其他任何操作之前,都要确保工作副本是干净的.使用 git status 进行查找.如果有未提交的更改,请将其保存在存储中( git stash 或在以下位置提交)临时分支.
  2. 运行 git branch backup 在当前提交上创建一个备份分支.
    确实不是必需的(使用 reflog 或通过写下当前提交的哈希值),但这是在出现问题时恢复当前状态的最简单方法.
  3. 运行 git reset --soft HEAD~2 .
    这会将HEAD移至过去的两次提交(在提交e8de117上),而无需更改工作树或索引.索引和工作树现在看起来就像刚创建提交6aa74e4之前的样子.最近两次提交中更改的所有文件都将已添加到索引中.因为HEAD位于e8de117上,所以下一个提交将在e8de117之上创建(它将替换"提交6aa74e477c15d6).
  4. 运行 git commit .您必须输入新的提交消息.
  5. 如果结果不符合您的预期( git diff backup 报告任何差异),或者如果您改变了主意,请运行 返回您从哪里开始(实际上是在第2步之后).
  6. 如果您对输出感到满意,请删除在第2步中创建的backup分支( git branch -D backup ).
  7. 仅此而已.
  1. No matter the approach, before anything else make sure the working copy is clean. Use git status to find out. If there are uncommitted changes either save them in a stash (git stash or commit them on a temporary branch.
  2. Run git branch backup to create a backup branch on the current commit.
    It is not really needed (the same result can be achieved using the reflog or by writing down the hash of the current commit) but it is the easiest way to restore the current status if something goes wrong.
  3. Run git reset --soft HEAD~2.
    This moves HEAD two commits in the past (on commit e8de117) without changing the working tree or the index. The index and the working tree look now like they were just before you created the commit 6aa74e4. All the files changed in the last two commits will be already added to the index. Because HEAD is on e8de117, the next commit will be created on top of e8de117 (it will "replace" commits 6aa74e4 and 77c15d6).
  4. Run git commit. You'll have to enter a new commit message.
  5. If the result doesn't look like you expected (git diff backup should not report any difference) or if you have changed your mind, run git reset --hard backup to go back where you started from (actually, right after step #2).
  6. If you are pleased with the output then remove the backup branch created at step 2 (git branch -D backup).
  7. That's all.

这篇关于如何合并最后两个提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆