Git:推送修改后的提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3968281/
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
Git: pushing amended commits
提问by mikewilliamson
I am currently working on a project and using machines in two different locations to do it. I have created a branch for the feature I am working on and when I finish some work on it I amend my commit to that branch and push it to the server so I can pick up where I left off on my other machine.
我目前正在做一个项目,并在两个不同地点使用机器来完成它。我已经为我正在处理的功能创建了一个分支,当我完成一些工作时,我修改我对该分支的提交并将其推送到服务器,这样我就可以在我的另一台机器上继续我离开的地方。
When I try to send my amended commit it rejects my push. I assume this is because I am pushing up a commit that is intended to clobber the current HEAD of the feature branch. I typically just use --force.
当我尝试发送修改后的提交时,它拒绝了我的推送。我认为这是因为我正在推送一个旨在破坏功能分支的当前 HEAD 的提交。我通常只使用--force。
Is there a better way to do this?
有一个更好的方法吗?
mike@sleepycat:~/projects/myproject$ git pull origin topx
From heroku.com:myproject
* branch topx -> FETCH_HEAD
Already up-to-date.
mike@sleepycat:~/projects/myproject$ git add app/models/reward.rb
mike@sleepycat:~/projects/myproject$ git commit --amend
[topx 82a9880] Added topX reward
9 files changed, 106 insertions(+), 21 deletions(-)
rewrite app/views/ceo/_reward_criteria.html.erb (96%)
create mode 100644 public/javascripts/jquery.multiselect.min.js
create mode 100644 public/site/javascripts/jquery.multiselect.min.js
create mode 100644 public/stylesheets/jquery.multiselect.css
mike@sleepycat:~/projects/myproject$ git push origin topx
To [email protected]:myproject.git
! [rejected] topx -> topx (non-fast-forward)
error: failed to push some refs to '[email protected]:myproject.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
回答by max
There is no way to overwrite remote branch without --force in this case. You have 2 options:
在这种情况下,没有 --force 就无法覆盖远程分支。您有 2 个选择:
- continue to use
git push --force
- don't use
--amend
. If some work you've done deserves a separate commit (is stable enough, compiling, passing more tests, etc...), create new commit. If not, create temporary branch (something likemy-feature-unstable
if your branch ismy-feature
) and commit there. When it becomes stable again, you can dogit reset --soft my-feature
,checkout my-feature
andcommit
to create single commit in your branch from several unfinished commits. After that, temporary branch can be deleted.
- 继续使用
git push --force
- 不要使用
--amend
. 如果您所做的某些工作值得单独提交(足够稳定、编译、通过更多测试等),请创建新提交。如果没有,请创建临时分支(类似于my-feature-unstable
您的分支是my-feature
)并在那里提交。当它再次稳定时,您可以执行git reset --soft my-feature
,checkout my-feature
并commit
从多个未完成的提交在您的分支中创建单个提交。之后,可以删除临时分支。