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


当前回答

方法1 如果你有很多承诺

git rebase -i master 然后按一下键盘“i”来编辑

你会看到这样的:

pick etc1
pick etc2
pick etc2

用“f”取代“ pick”这个词,然后按 esc y :wq。

pick etc1 //this commit will the one commit
f etc2
f etc2

按下这个命令

git push origin +head

方法2 如果你有少数承诺,你可以这样做去删除承诺,你必须这样做去删除你的第二承诺等等。

git reset --soft HEAD^1 // or git reset --soft head~1
git commit --amend //then press `:wq` 
git push -f

方法 3 如果你已经有一个承诺,你不想提交另一个承诺更多

git add files...
git commit --amend  //then press `:wq`
git push origin +head

其他回答

您可以按照 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....

详细信息点击链接

这是超级可爱的,但在某种寒冷的方式,所以我只会把它推到戒指中:

GIT_EDITOR='f() { if [ "$(basename $1)" = "git-rebase-todo" ]; then sed -i "2,\$s/pick/squash/" $1; else vim $1; fi }; f' git rebase -i foo~5 foo

翻译:为 git 提供一个新的“编辑器”,如果要编辑的字体名称是 git-rebase-todo(互动的 rebase prompt) 会改变所有,但第一个“点”到“squash”,否则 spawns vim - 这样,当你被要求编辑破碎的承诺消息时,你会得到 vim。

也许我会做马克·隆加尔所建议的事情。

最方便的方式是使用 GitHub 桌面. 只需在历史中选择所有命令,右键单击并选择“Squash x 命令”:

此分類上一篇

您可以使用 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 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。

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

git rebase -i HEAD~3


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


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

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

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