2013-07-16 81 views
7

我想用提交消息「第一」和「第二」壓縮兩個最新的提交。首先,我拉主人然後我用命令想壓縮github中的多個提交

git rebase -i HEAD~2 master 

這表明我都犯這樣的編輯:

pick first 
pick second 

然後我改變這個編輯器:

pick first 
squash second 

保存後我得到這個消息的變化:

Successfully rebased and updated refs/heads/master. 

它確實改變了遠程主機中的任何東西。要應用這些更改我使用git push命令,並得到了以下錯誤:

To https://github.com/aneelatest/GITtest.git 
! [rejected]  master -> master (non-fast-forward) 
error: failed to push some refs to 'https://github.com/test/GITtest.git' 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
hint: before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

然後我再次運行git pull命令,並將其合併起源主人,讓另一個與此提交的提交信息:

Merge branch 'master' of https://github.com/aneelatest/GITtest 

在此之後,當我運行git push時,它將兩個提交壓縮成一個消息「first」。 的問題是,在遠程主,我現在有四次提交:

first 
second 
Merge branch 'master' of https://github.com/test/GITtest 
first 

,我想只有一個承諾這是一個壓扁與提交信息「第一」。 任何想法,我犯了錯誤?

+0

您不必擠壓:業主可以爲您做(自2016年3月起):請參閱http://stackoverflow.com/a/36377439/6309 – VonC

回答

9

git rebase重寫歷史記錄,因爲提交已被修改。當所述提交沒有被推送到遠程存儲庫時,這不是問題,但是這裏的遠程以前是用你改寫的提交來推送的,所以它拒絕了推送。

當你從github中拉出時,它將兩個歷史合併在一起,並且應用了你的壁球提交,因此這個混亂。

結論:當你想重寫已被推送的提交,你有兩個選擇:

  • 不這樣做
  • git push --force,這將改寫歷史也遙控器上,並告訴人們你已經重寫了歷史記錄,所以他們會在他們的下一次拉動中看到奇怪的事情。