我如何将最后的N承诺集成到一个承诺中?


当前回答

您可以按照 git 命令使用。

 git rebase -i HEAD~n

n(=4在这里)是最后的承诺的数字,然后你得到了以下选项,

pick 01d1124 Message....
pick 6340aaa Message....
pick ebfd367 Message....
pick 30e0ccb Message....

更新如下,选择一个承诺,并将其他人推到最新的,

p 01d1124 Message....
s 6340aaa Message....
s ebfd367 Message....
s 30e0ccb Message....

详细信息点击链接

其他回答

在问题上,可以是双重的“最后”是什么意思。

* commit H0
|
* merge
|\
| * commit B0
| |
| * commit B1
| | 
* | commit H1
| |
* | commit H2
|/
|

可以使用折扣,但以不同的方式,然后在其他提到的答案:

首頁 / 首頁 / 2

这将向您展示选择选项(如其他答案中提到的):

pick B1
pick B0
pick H0

pick B1
pick B0
s H0

在保存和退出后,将执行命令,然后在H1之后,这意味着它将要求你重新解决冲突(在那里,主将是H1首先,然后积累命令,当它们被应用)。

退缩结束后,您可以选择失败的 H0 和 B0 消息:

* commit squashed H0 and B0
|
* commit B1
| 
* commit H1
|
* commit H2
|

P.S. 如果你只是做一些重新设置到BO:(例如,使用重新设置 - 混合,这里详细解释 https://stackoverflow.com/a/18690845/2405850):

git reset --mixed hash_of_commit_B0
git add .
git commit -m 'some commit message'

您可以使用 git merge --squash 为此,这比 git rebase -i 略有优雅。

警告: 首先,请确保您完成工作 - 检查 git 状态是干净的(因为 git 重新设置 - 硬将抛弃错误和错误的变化)

然后:

# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12

# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}

# Commit those squashed changes.  The commit message will be helpfully
# prepopulated with the commit messages of all the squashed commits:
git commit

git merge 的文档详细描述了 --squash 选项。


更新:这个方法的唯一真正优点,比克里斯·约翰森(Chris Johnsen)在他的回答中提出的更简单的吉特重新设置 --soft HEAD~12 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。

让我们说 n 真的很大. 你不想处理每一个承诺. 你只是想要一个名为 new_feature_branch 的新分支,其中有 1 承诺和所有旧_feature_branch 的变化。

git checkout main 

# will contain changes all changes from old_feature_branch 
git checkout -b new_feature_branch 

# get all changes from old_feature_branch and stage them
for f in $(git --no-pager diff --name-only old_feature_branch ) ; do git checkout fencing -- $f ; done

git commit -m "one commit message"

首先,我知道我的功能分支和当前主分支之间的承诺数量。

git checkout master
git rev-list master.. --count

然后,我创建另一个基于我的功能分支的分支,保持我的功能分支无触。

最后,我跑

git checkout my-feature
git checkout -b my-rebased-feature
git checkout master
git checkout my-rebased-feature
git rebase master
git rebase head^x -i
// fixup/pick/rewrite
git push origin my-rebased-feature -f // force, if my-rebased-feature was ever pushed, otherwise no need for -f flag
// make a PR with clean history, delete both my-feature and my-rebased-feature after merge

希望能帮忙,谢谢。

感谢这个有用的博客帖子,我发现你可以使用这个命令来破坏过去的3个任务:

git rebase -i HEAD~3


显示的文档是相当清晰的. 在任何指定线上,您可以将命令从选择到您选择的命令更改。


我做的第一件事是纠正错误,并在评论中做出新的承诺,把这一点推到我的新功能中。

接下来,我将包含修复(fe7f1e0)的承诺移动到属性承诺下方,并将选择转换为修复。

这在所有承诺都是本地的时工作得很好,但如果你试图改变已经推到远程的承诺,你真的可能会给那些已经检查过同一个分支的其他骗子带来问题!