2013-06-18 113 views
0

我剛剛推送了一個提交,然後意識到我需要更改提交消息。修改推送提交消息

所以在我的本地回購我所做的:

git commit --amend -m "New commit message" 

但是,當我再試圖推動這一點,我得到錯誤信息的負荷說

Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') and try again 

這是錯誤的方式修改我的信息?最後,我最終不得不重置我的所有回購協議,然後再次提交新的消息。

所以我的問題是,什麼是正確的方法來修改已經推動的東西的提交信息?

回答

2

簡答:沒有正確的方法。

git commit --amend的作用是用相似但改變的提交「替換」前一個提交。你並沒有真正改變原來的提交。它仍然存在,但沒有再引用它,它最終將被垃圾收集,除非開始引用它。

在本地完成時,這是透明的。但是當你推動提交時,實質上已經太晚了。您已經與其他人分享了可能已經提交併基於該提交的工作的提交。您不能用另一個提交來替換該提交。

比方說你犯了一個提交A(後提交B):

B - A <- master 

然後你改變了主意,修改A,實際上將創建一個新的提交A」。當前分支將指向這個新的提交。原來犯了仍然存在,但沒有分支指向它

B - A 
    \ 
    A' <- master 

如果先推了

local       remote 
B - A <-master     B - A <- origin/master 

,然後修改,你將不會被允許進行正常推進,因爲這推不會是一個快進合併

local       remote 
B - A       B - A <- origin/master 
    \ 
    A' <- master 

Excacerbating問題:別人可能已經使用了您提交

local       remote 
B - A       B - A - C <- origin/master 
    \ 
    A' <- master 

你可以做你的修改,然後做推力git push -f。但是這會對其他開發人員造成問題,這些開發人員的工作基於原始提交。這是與重新分配相同的問題(一個commit --amend有點像一個小型rebase)。有關詳細說明,請參閱"The Perils of Rebasing" section of the Git Pro book

local       remote 
B - A       B - A - C 
    \        \ 
    A' <- master     A' <- origin/master 
0

您可以做git push --force,但它可能會制動您的存儲庫。由於某種原因,Git哲學反對改變歷史。