2010-10-19 92 views
9

我目前正在開發一個項目,並在兩個不同的位置使用機器來完成它。我爲我正在工作的功能創建了一個分支,當我完成一些工作時,我修改了對該分支的提交併將其推送到服務器,以便我可以從我的另一臺計算機上停下來。Git:推動修改的提交

當我嘗試發送修改後的提交時,它拒絕了我的推送。我認爲這是因爲我推送了一個旨在摧毀功能分支的當前HEAD的提交。我通常只使用--force。

有沒有更好的方法來做到這一點?

[email protected]:~/projects/myproject$ git pull origin topx 
From heroku.com:myproject 
* branch   topx  -> FETCH_HEAD 
Already up-to-date. 
[email protected]:~/projects/myproject$ git add app/models/reward.rb 
[email protected]:~/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 
[email protected]:~/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. 

回答

17

沒有辦法覆蓋遠程分支,而不在這種情況下--force。你有兩個選擇:

  • 繼續使用git push --force
  • 不使用--amend。如果你做的一些工作值得單獨提交(足夠穩定,編譯,傳遞更多測試等),則創建新的提交。如果沒有,創建臨時分支(如my-feature-unstable,如果您的分支是my-feature)並在那裏落實。當它再次變得穩定時,您可以執行git reset --soft my-feature,checkout my-featurecommit在您的分支中從幾個未完成的提交中創建單個提交。之後,可以刪除臨時分支。
+6

我認爲「不要使用修改」選項:不要在已發佈的工作中使用它。在未發表的作品(本地/個人分支)上,你絕對應該使用它。 (如果你有一個以上的個人回購,那就意味着你會用'push-force'來轉移你的工作,這很好。) – Cascabel 2010-10-19 13:20:50

+0

非常感謝!現在我可以保證我的代碼安全,提交日誌清晰。 – 2015-10-13 13:11:45