如何在推送后压缩git中的提交?

这给出了压缩多个提交的一个很好的解释:

http://git-scm.com/book/en/Git-Branching-Rebasing

但它不适用于已被推送的提交。 如何在我的本地和远程回购中压缩最近几次提交?

编辑:当我做git rebase -i origin/master~4 master ,保留第一个作为pick ,将其他三个设置为squash ,然后退出(通过emacs中的cx cc),我得到:

$ git rebase -i origin/master~4 master
# Not currently on any branch.
nothing to commit (working directory clean)

Could not apply 2f40e2c... Revert "issue 4427: bpf device permission change option added"
$ git rebase -i origin/master~4 master
Interactive rebase already started

其中2f40是pick提交。 现在git log没有出现4个提交。 我希望我的编辑器能够重新启动,以便我可以输入提交消息。 我究竟做错了什么?


#1楼

在一个分支上,我能够这样做(最近4次提交)

git checkout my_branch
git reset --soft HEAD~4
git commit
git push --force origin my_branch

#2楼

只需创建一个branch来处理和不在 master上工作,就可以避免很多问题:

git checkout -b mybranch

以下作品remote提交已推及的混合物remote推送的提交/ local唯一的承诺:

# example merging 4 commits

git checkout mybranch
git rebase -i mybranch~4 mybranch

# at the interactive screen
# choose fixup for commit: 2 / 3 / 4

git push -u origin +mybranch

我也有一些拉请求说明可能会有所帮助。


#3楼

git rebase -i master

你会得到编辑器vm open和msgs这样的东西

Pick 2994283490 commit msg1
f 7994283490 commit msg2
f 4654283490 commit msg3
f 5694283490 commit msg4
#Some message 
#
#some more

在这里,我已将所有其他提交的选择更改为“f”(代表修正)。

git push -f origin feature/feature-branch-name-xyz

这将修复所有提交到一个提交,并将删除所有其他提交。 我做到了这一点,它帮助了我。


#4楼

接受答案的细微差别,但我在挤压时遇到了很多困难,最后得到了它。

$ git rebase -i HEAD~4
  • 在打开了交互式屏幕,请用南瓜顶部为所有要压扁提交挑选
  • 通过esc --> :wq保存并关闭编辑器

使用以下方式推送到遥控器

$ git push origin branch-name --force

#5楼

为了压缩两个提交,其中一个已被推送,在一个分支上,以下工作:

git rebase -i HEAD~2
    [ pick     older-commit  ]
    [ squash   newest-commit ]
git push --force

默认情况下,这将包括最新提交的提交消息作为对旧提交的注释。


#6楼

1)git rebase -i HEAD~4

To elaborate: It works on the current branch; the HEAD~4 means squashing the latest four commits; interactive mode (-i)

2)此时,编辑器打开,使用提交列表,更改第二次和后续提交,用壁球替换选择然后保存。

输出:成功重新定位并更新refs / heads / branch-name。

3)git push origin refs / heads / branch-name --force

输出:... remote:remote:要为branch-name创建合并请求,请访问:remote: http:// xxx / sc / server / merge_requests / new?merge_request%5Bsource_branch%5D = sss remote:to ip:sc /server.git + 84b4b60 ... 5045693 branch-name - > branch-name(强制更新)


#7楼

壁球在当地提交

git rebase -i origin/master~4 master

然后用力推动

git push origin +master

--force+之间的区别

git push的文档:

请注意, - --force适用于所有被推送的引用,因此使用它将push.default设置为matching或使用remote.*.push配置的多个推送目的地remote.*.push可能会覆盖当前分支以外的引用(包括本地引用)严格落后于他们的远程对手)。 要强制推送到一个分支,请使用refspec前面的+来推送(例如git push origin +master强制推送到master分支)。


#8楼

当您使用Gitlab或Github时,您可以通过这种方式遇到麻烦。 您可以使用上述方法之一压缩提交。 我最喜欢的是:

git rebase -i HEAD~4
or
git rebase -i origin/master

为你的提交选择壁球或修正。 此时,您将检查git状态。 信息可能是:

    On branch ABC-1916-remote
    Your branch and 'origin/ABC-1916' have diverged,
    and have 1 and 7 different commits each, respectively.
      (use "git pull" to merge the remote branch into yours)

你可以试着拉它。 不要那么做,否则你将和以前一样。

取而代之的是:

git push origin +ABC-1916-remote:ABC-1916

+允许强制只推送到一个分支。

发布了0 篇原创文章 · 获赞 2 · 访问量 4983

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/104037641