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


当前回答

程序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 命令使用。

 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'

例如,如果您想在一个分支(远程存储库)中排除最后3个承诺,例如: https://bitbucket.org

我所做的就是

git reset --soft HEAD~3 git commit git push 起源 <branch_name> --force

您可以使用 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 -i 如何总是让我与承诺命令混淆 - 老到新或相反? 所以这就是我的工作流:

git rebase -i HEAD~[N], N 是我想加入的命令的数量,从最新的命令开始。 所以 git rebase -i HEAD~5 意味着“将最后 5 命令分成一个新的命令”; 编辑打开,显示我想合并的命令列表。

来源和其他阅读: #1, #2。