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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10054611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:23:51  来源:igfitidea点击:

How to push new (rewritten) history to remote repository

gitgithub

提问by NightOwl888

Basically, I have an open pull request that I want to fix and at the same time I want to make 1 commit that contains 2 features into 2 seperate commits.

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

Github repository now looks like this where fix is a new branch:

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

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

I created a pull request from fix.

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

I wanted to change the last commit in fix into 2 commits in my local repository as follows:

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

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

where n-n are my 2 new commits.

其中 nn 是我的 2 个新提交。

To get to this point locally, I did this:

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

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"

So now I have 2 commits the way I want. The problem is I found a bug that ended up in the pull request. Since I have already pushed to the remote repository, it won't accept my new commits (as the original one is now missing).

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

I run:

我跑:

git push origin fix --dry-run

and I get the message:

我收到消息:

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

I have seen other posts suggest to pull my changes from origin before pushing back again, but won't that basically reset my 2 commits back into the one?

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

Ideally, what I would like to do is assign the same commit ID to the last commit so it can replace the current one as is. Is there a way to do that? (Note that I didn't run git resetwith --hard)

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

回答by Nate

When faced with that problem, a force push has worked for me:

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

git push --force origin fix