git - 如何检出两次提交之间所有更改的文件

git - How to checkout all changed files between two commits

我有一个分支 A 和一个分支 B。分支 B 比 A 早三个提交。

我可以使用git diff列出A和B之间更改的文件。

但我的问题是:当我在 A 上时,如何在 A 和 B 之间检出所有那些更改的文件,然后将它们作为一次提交一起提交到 A 中?

将提交变基到 A 上,然后使用

将它们压缩到一个提交中
git rebase -i HEAD~3

通过rebase获取B的提交

git rebase B

现在将三个提交压缩为一个

git rebase -i HEAD~3

只需将 B 分支拉入 A,然后将最后三个提交合并为一个。

$ git checkout A
$ git pull origin B
$ git log
# Now top 3 commits are B's commit

# now back to 3 commits but exists all changes of that 3 commits (soft reset)
$ git log
# copy the last commit-hash of A (before pulled)                    

$ git reset --soft <commit-hash>

# now do one commit with all changes
$ git add .
$ git commit -m 'add last 3 commits of B as one in A'
$ git push origin HEAD         # push the changes to remote

如果 B 严格领先于 A(而不是落后于它),那么您可以简单地 运行 "merge --squash":

git merge --squash B
git commit