最基礎的 git 指令被隱藏在先前的後端小菜鳥學習筆記內,有機會再把他整理出來
後端小菜鳥學習筆記系列是當初上班太忙碌,資訊又不斷的往我的腦袋塞(雖然現在也差不多啦 XDDDD)
然後暫時還想不出更好的寫作方法時,只好利用日記記錄去記錄每一天
造成每天都很長,且夾雜一堆不同的資訊的文章出現
雖然當初確實是想要紀錄每天每天的練習,可是現在往回看會發現
這邊紀錄些 git 用法,但還是強烈建議知道原理,比記每個指令會好一點
git log -p
# 強制推上github
git push -f
# 在還未push上github前, 觀看更改前與更改後的不同
git diff
# 在還未push上github前, reset所有更改紀錄
git reset --hard
# 可去抓遠端的branch
$ git fetch --all
$ git checkout <branch>
講一下 squash, 因為我有慘痛經驗 QQ
做 squash,是將很多次 commit 整併/合併成一次(或幾次)的動作
假設你 commit 18 次(如我), 中間都是很多只有細小改動,那就可以使用 squash 去整併成一個 commit
# 利用log 來找出要整併的分支的前一個動作版本碼 git log
$ git log
會看到很多個commit的紀錄
commit xxxxxxxxxxxx__d394g04329oooooooooooooooo (版本號碼)
...
...
commit xxxxxxxxxxxx____h949k929d031__oooooooooo
...
...
找到你第一個 commit 的前一個 commit 的版本碼當 base 基底 因為你要將所有你的 commit 做合併,所以你必須抓在你還沒修改任何程式碼 還沒做任何 commit 的那一個版本碼!!
找到版本碼後,下這個指令來進行 squash 動作 git rebase -i [版本碼]
$ git rebase -i [版本碼]
此時會進到一個畫面
pick 01d1124 Complete feature.
pick 6340aaa Rewrite Unit Text.
pick ebfd367 Add new function.
pick 30e0ccb Seperate function to other file.
# Rebase 60709da..30e0ccb onto 60709da
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
下方會有做更改的解釋
squash 簡寫是 s
將先前的 commit 做 squash 合併,僅留最後一個 commit
pick 01d1124 Complete feature.
squash 6340aaa Rewrite Unit Text.
squash ebfd367 Add new function.
squash 30e0ccb Seperate function to other file.
改好後存檔離開
:wq
接著會跳到下一個頁面,這邊是要輸入合併後的 commit 訊息要是什麼
# your commit_1 message
第一個訊息
# your commit_2 message
第二個
# your commit_3 message
...
# your commit_4 message
...
# your commit_5 message
若是我,我就將這些訊息砍掉後,寫一行(或者多行),解釋你這個 commit 做了什麼事
寫完以後存檔離開
:wq
將新更改好的 commit 要強制推上
$ git push -f
若這中間不小心錯掉了,可以用這個指令來捨去這個 squash
$ git rebase --abort
他有提供下列兩個指令,不過我看不懂啦 XD
$ git rebase --edit-todo
$ git rebase --continue
再講一個,當你開發的分支 branch behind 進度的時候怎麼辦呢?
# 先切到 主支做更新進度
$ git checkout [主進度的主支]
$ git pull
# 將分支原先的基底(就是從舊進度繼續做),更新新的基底
# 用rebase
$ git checkout [你的分支]
$ git rebase [主進度的主支]
就將分支的進度接到目前最新進度上囉!
參考資料
http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html
找資料時,找到一個很厲害的 blog