如何将新的(重写的)历史记录推送到远程存储库

问题描述 投票:0回答:1

基本上,我有一个想要修复的开放拉取请求,同时我想将包含 2 个功能的 1 个提交分成 2 个单独的提交。

Github 存储库现在看起来像这样,其中修复是一个新分支:

master c-c-c
            \
     fix c-c-c-c

我从修复创建了一个拉取请求。

我想将修复中的最后一次提交更改为本地存储库中的 2 次提交,如下所示:

master c-c-c
            \
     fix c-c-c-n-n

其中 n-n 是我的 2 个新提交。

为了在本地达到这一点,我这样做了:

1. git rebase -i HEAD~2
2. Changed my last commit line to "edit", saved and closed the file
3. git reset HEAD^
4. git stash save
5. Removed the changes I don't want in the first commit
6. git commit -m "commit a" -a
7. git stash apply
8. git commit -m "commit b"

所以现在我按照我想要的方式进行了 2 次提交。问题是我发现了一个错误,最终出现在拉取请求中。由于我已经推送到远程存储库,它不会接受我的新提交(因为原始提交现在丢失了)。

我跑步:

git push origin fix --dry-run

我收到消息:

To [email protected]:<UserName>/<Repository>.git
! [rejected]        fix -> fix (non-fast-forward)
error: failed to push some refs to '[email protected]:<UserName>/<Repository>.git'

我看到其他帖子建议在再次推回之前从原点拉出我的更改,但这不会基本上将我的 2 次提交重置回原来的状态吗?

理想情况下,我想做的是将相同的提交 ID 分配给最后一次提交,以便它可以按原样替换当前的提交 ID。有没有办法做到这一点? (请注意,我没有用

git reset
运行
--hard

git github
1个回答
92
投票

当面对这个问题时,强力推动对我有用:

git push --force origin fix

编辑:根据下面的评论,

--force-with-lease
优于直接
--force
,因为它包含一些保护措施,以防出现干预提交。 这篇文章将其描述为“——用安全带强制”

© www.soinside.com 2019 - 2024. All rights reserved.