想要在github中压缩多个提交

use*_*869 7 github

我想用提交消息"first"和"second"来压缩两个最新的提交.首先我拉主人然后我使用命令

git rebase -i HEAD~2 master
Run Code Online (Sandbox Code Playgroud)

它向我展示了这样一个编辑器中的两个提交:

pick first
pick second
Run Code Online (Sandbox Code Playgroud)

然后我将此编辑器更改为:

pick first
squash second
Run Code Online (Sandbox Code Playgroud)

保存更改后,我收到此消息:

Successfully rebased and updated refs/heads/master.
Run Code Online (Sandbox Code Playgroud)

它确实改变了远程主人的任何东西.要应用这些更改,我使用该git push命令并收到以下错误:

To https://github.com/aneelatest/GITtest.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/test/GITtest.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

然后我再次运行该git pull命令,它合并origin master并使用此提交消息进行另一次提交:

Merge branch 'master' of https://github.com/aneelatest/GITtest
Run Code Online (Sandbox Code Playgroud)

在此之后,当我运行git push时,它会将两个提交压缩成一个带有消息"first"的提交.问题是在远程主站中,我现在有四个提交:

first
second
Merge branch 'master' of https://github.com/test/GITtest
first
Run Code Online (Sandbox Code Playgroud)

我想要的只有一个提交是压缩的提交消息"first".我做错的任何想法?

Cha*_*esB 9

git rebase重写历史记录,因为提交已被修改.当所述提交没有被推送到远程存储库时,这不是一个问题,但是这里的遥控器先前已经被你重写的提交推送了,所以它拒绝了推送.

当你从github撤出时,它合并了两个历史记录,并应用你的壁球提交,因此混乱.

结论:当您想要重写已经推送的提交时,您有两个选择:

  • 不要这样做
  • 做一个git push --force,它也会在遥控器上重写历史,并告诉人们你重写了历史,所以他们会在下一次拉动时看到奇怪的东西.