git - merge 提交不会出现在 git rebase --interactive

标签 git git-interactive-rebase

git log 显示以下内容:

commit 1abcd[...]
Author: [...]
Date: [...]

    [Useful commit]

commit 2abcd[...]
Author: [...]
Date: [...]

    Merge branch [...] of [etc. etc.]

commit 3abcd[...]
Author: [...]
Date: [...]

    [Useful commit]

那个 merge 提交对我来说没用——它不代表分支的有意义的状态并且是从远程 pull 生成的,所以我有远程历史的真实提交——不需要提交来标记事实我 pull 的。我想压缩这个 merge 提交。我常用的 Squash 技术是:

git rebase --interactive HEAD~2(或者无论我需要多远)

然后我会将其压缩到相邻的提交中。我有时会这样做,例如,如果我进行了提交,意识到我错过了一个非常重要的细节(单个文件,或者没有更改其中一个文件中的一行),然后进行另一次提交,这基本上只是一个快速的 oops。这样,当我将更改推送回远程时,一切都很好、很干净,并讲述了一个连贯的叙述。

但是,在这种情况下,当我运行 git rebase ... 命令时,commit 2abcd 没有出现!它似乎直接跳过 2abcd,而是显示 1abcd3abcd。 merge 提交是否有什么特别之处阻止它出现在 git rebase --interactive 中?我还可以使用什么其他技术来压缩该 merge 提交?

根据@Cupcake 的要求

更新:

git log --graph --oneline --decorate 的输出如下所示:

* 1abcd (useful commit)
* 2abcd (merge)
|  \ <-- from remote
|   * 3abcd (useful commit)
|   |

有帮助吗?

最佳答案

如果没有 --preserve-merges

,Rebase 通常不会保留 merge 提交

好的,所以我不确定如果您尝试使用带有 --preserve-merges 的交互式 rebase 来压缩 merge 提交会发生什么......但这就是我要删除的方式在你的情况下 merge 提交并使你的历史线性化:

  1. 在远程分支之上的 merge 提交之前对所有内容进行 rebase 。

  2. 在 merge 提交之后,在先前重新设置基础的提交之上挑选或重新设置所有内容。

如果在 merge 提交后只有 1 次提交

所以就命令而言,它看起来像这样:

# Reset to commit before merge commit
git reset --hard <merge>^

# Rebase onto the remote branch
git rebase <remote>/<branch>

# Cherry-pick the last commit
git cherry-pick 1abcd 

如果在 merge 提交后有超过 1 次提交

# Leave a temporary branch at your current commit
git branch temp

# Reset to commit before merge commit
git reset --hard <merge>^

# Rebase onto the remote branch
git rebase <remote>/<branch>

# Cherry-pick the last commits using a commit range.
# The start of the range is exclusive (not included)
git cherry-pick <merge>..temp

# Alternatively to the cherry-pick above, you can instead rebase everything
# from the merge commit to the tip of the temp branch onto the other
# newly rebased commits.
#
# You can also use --preserve-merges to preserve merge commits following
# the first merge commit that you want to get rid of...but if there were
# any conflicts in those merge commits, you'll need to re-resolve them again.
git rebase --preserve-merges --onto <currentBranch> <merge> temp

# These next steps are only necessary if you did the rebase above,
# instead of using the cherry-pick range.
#
# Fast-forward your previous branch and delete temp
git checkout <previousBranch>
git merge temp
git branch -d temp

文档

关于git - merge 提交不会出现在 git rebase --interactive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24786404/

相关文章:

node.js - 在 Node 中执行子进程来控制控制台

Git 打开默认的 git 编辑器而不是 VIM 或任何其他编辑器 (Git Rebase)

Git:如何跳到 rebase 的末尾并保留到目前为止的所有更改?

windows - 有什么办法可以半自动提交吗?

bash - git log 和 git rev-list 的不同结果

git - checkout 远程分支时丢失本地提交

git - 如何将最后一次提交 rebase (压缩)到 master 并更改提交消息

git - 在 git-grep 输出中包含列号

git - 为什么交互式 rebase 将我的提交压缩在一起?

git rebase --interactive (reword) 给我留下了两棵具有相同提交信息的树