[git] 只想push某一筆特定commit的方法

當需要push特定commit時可以用

git push <remotename> <commit SHA>:<remotebranchname>

但這個指令會push此commit之前所有的commit(包含此coomit)
若需push單獨commit必須併用git rebase -i調換commit順序

commit 0a288af92cfca64046caacb644a8a324e2832d7f
Author: hanschang <hanschang@synology.com>
Date:   Tue Apr 14 09:44:16 2015 +0800
 
    <commit> #c
 commit 32116c884885118484e2565eac18a0433ac6fc6f
Author: hanschang <hanschang@synology.com>
Date:   Wed Apr 15 15:36:45 2015 +0800
 
    <commit> #b
commit 6e7b5350df47f49e0dbbe67a210c337cb130b209
Author: hanschang <hanschang@synology.com>
Date:   Wed Apr 15 14:16:49 2015 +0800
 
    <commit> #a

假設有三筆commit依順為a, b, c

a已push b,c尚未push

 情境一:

若我們想push b但不要push c
git push orgin 32116c884885118484e2565eac18a0433ac6fc6f:master即可達成目標

 情境二:

若我們想push c但不要push b,這時必須用git rebase -i 6e7b5350df47f49e0dbbe67a210c337cb130b209交換c,b
這時會出現如下的檔案
 =============================================================
  GNU nano 2.2.6                  檔案: /xxxxx/.git/rebase-merge/git-rebase-todo
pick 32116c8 <commit> #c
pick 0a288af <commit> #b
# Rebase 6e7b535..0a288af onto 6e7b535
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like “squash”, but discard this commit’s log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
 =============================================================

我們只需要將
pick 32116c8 <Router> #c
pick 0a288af <Router> #b

這兩行的順序調換如下即可:
pick 0a288af <Router> #b
pick 32116c8 <Router> #c

PS. git rebase -i也可以修改多個commit的內容
只要將pick改為edit之後就可以修改檔案
檔案修改後輸入git commit –amend完成commit的修改
之git rebase –continue就可以繼續改下一個commit
若沒有下一個commit要修改rebase就會結束

發表留言

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料