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

Nig*_*888 37 git github

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

Github存储库现在看起来像修复是一个新的分支:

master c-c-c
            \
     fix c-c-c-c
Run Code Online (Sandbox Code Playgroud)

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

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

master c-c-c
            \
     fix c-c-c-n-n
Run Code Online (Sandbox Code Playgroud)

其中nn是我的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"
Run Code Online (Sandbox Code Playgroud)

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

我跑:

git push origin fix --dry-run
Run Code Online (Sandbox Code Playgroud)

我得到的消息是:

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'
Run Code Online (Sandbox Code Playgroud)

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

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

Nat*_*ate 71

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

git push --force origin fix
Run Code Online (Sandbox Code Playgroud)

  • 如果已经撤消了更改,项目所有者将需要重置,因为提交仍将存在于他的分支中.`git fetch origin`然后`git reset --hard origin/master`.请参阅:http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head (4认同)
  • 使用--force-with-lease而不是--force:仅当没有中间提交时才强制使用。参见:https://developer.atlassian.com/blog/2015/04/force-with-lease/ (3认同)