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

标签 git github

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

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

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

我从修复中创建了一个 pull 请求。

我想将 fix 中的最后一次提交更改为本地存储库中的 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 次提交。问题是我发现了一个最终出现在 pull 请求中的错误。由于我已经推送到远程存储库,它不会接受我的新提交(因为原始提交现在丢失了)。

我跑:

git push origin fix --dry-run

我收到消息:

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

我看到其他帖子建议在再次推回之前从源中提取我的更改,但这不会基本上将我的 2 次提交重置为一次吗?

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

最佳答案

当遇到这个问题时,强制推送对我有用:

git push --force origin fix

关于git - 如何将新的(重写的)历史推送到远程存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054611/

相关文章:

git - Bitbucket 存储库从昨天开始返回 "remote: Too many invalid password attempts. Try logging in through the website."(403)

GitLab fork 远程公共(public) repo

github - 来自 RStudio : "The system cannot find the path specified" 的 Git 提交错误

git - 使用 Git Bash 在 Windows 上存储 GitHub 凭据

Gitlab merge 请求 :- Ignore few files while merging

android - 用于调试 Android 的 repo 'bisect'?

windows - 权限被拒绝(公钥)-Windows 上的 Git Bash

github - 在 Travis CI 中设置上游项目

git rebase 失败,命令 : git-merge-recursive 出现未知退出代码 (128)

git - 如何将新文件夹链接到 git 存储库