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


当前回答

要将最后10个承诺分为1个单一承诺:

git reset --soft HEAD~10 && git commit -m "squashed commit"

如果你也想更新远程分支与破碎的承诺:

git push -f

其他回答

程序1

(一)定义承诺短暂的哈希

# git log --pretty=oneline --abbrev-commit
abcd1234 Update to Fix for issue B
cdababcd Fix issue B
deab3412 Fix issue A
....

在这里,即使是 git log --oneline 也可以用来获得短暂的 hash。

# git rebase -i deab3412 

....
pick cdababcd Fix issue B
pick abcd1234 Update to Fix for issue B
....

....
pick cdababcd Fix issue B
squash abcd1234 Update to Fix for issue B
....

# git log --pretty=oneline --abbrev-commit
1122abcd Fix issue B
deab3412 Fix issue A
....

# git push origin +master

程序2

首先,添加所需的文件

git add <files>

git commit --fixup=OLDCOMMIT

现在这个创建了一个新的承诺在头顶与 fixup1 <OLDCOMMIT_MSG>。

其次,下令下令下令下令下令下令下令下令下令下令下令下令。

git rebase --interactive --autosquash OLDCOMMIT^

在这里 ^ 意味着以前的承诺到 OLDCOMMIT. 这个 rebase 命令在一个编辑器(vim 或 nano)上打开了互动窗口,我们不需要做任何事情,只是保存和输出是足够的. 因为转移到此的选项将自动移动最新的承诺到旧的承诺旁边,并将操作转换为 fixup(相当于 squash)。 然后 rebase 继续完成。

程序3

如果需要将新的更改添加到最后的承诺方式 - 更改可以使用 git-commit。

    # git log --pretty=oneline --abbrev-commit
    cdababcd Fix issue B
    deab3412 Fix issue A
    ....
    # git add <files> # New changes
    # git commit --amend
    # git log --pretty=oneline --abbrev-commit
    1d4ab2e1 Fix issue B
    deab3412 Fix issue A
    ....  

结论

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

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。

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

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

许多答案是基于Git Rebase命令,但在我的经验中,它对Git初学者来说有点复杂和先进。

假设你想排除最后3个命令,然后以下是步骤:

下载现行承诺ID: 使用 git log -1 --online 并记住当前状态的承诺ID(仅在您与 git reset 做错事的情况下) 返回 3 个承诺: 使用 git reset --soft HEAD~3 您将返回 3 个承诺(并忘记您以前做过这三个承诺) 做一个新的承诺: 现在简单地做 git commit -m <NEW_SINGLE_MESSAGE> 将自动结合 t

如果与 git reset 发生错误,您可以通过 git reset --soft <ORIGINAL_COMMIT> 返回原始状态。

让我们说 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"