201607201059Git 操作筆記
這裡記下我常用的一些 Git 相關操作
查詢檔案的 Commit 紀錄
顯示特定檔案的 Commit 紀錄,-p 表示顯示細節
$ git log -p file_name
查詢檔案中各行的 Commit 紀錄
顯示特定檔案各行的最新 Commit 紀錄
$ git blame file_name
將檔案回復最新 Commit 版本
回復已修改的檔案到最新 Commit 處,若 file_path 為目錄,則該目錄內有更動的檔案都會回復到最新的 Commit
$ git checkout -- file_path
產生與套用 patch
產生 patch
$ git show > patch.txt
套用 patch
$ git apply patch.txt
Clone 遠端分支 (Clone remote branch)
將遠端的分支 checkout 到本地
$ git checkout -b branch_name origin/branch_name
將本地分支推到遠端 (Push new branch to remote)
將本地新的分支推到遠端,-u 選項為 --set-upstream 的縮寫
$ git push -u origin branch_name
刪除遠端分支
如果 git 版本為 1.7 版以後,使用這個指令刪除遠端分支
git push origin --delete branch_name
舊版則使用
git push origin :branch_name
重新命名分支
對於本地的分支,要重新命名相當簡單,只需要帶上 -m 參數就可以了,不過使用前記得先 checkout 到其他分支
# Checkout to other branch $ git checkout master # Rename local branch $ git branch -m old_branch new_branch
接下來 rename 遠端分支,需要透過先刪除後推新的分支
# Delete remote old branch $ git push origin --delete old_branch # Push new local branch $ git push -u origin new_branch
最後,其他已經 Clone 的本地 repositories,需要先刪除本地的舊分支,更新遠端列表後再 checkout 到新分支
# Delete old local branch $ git branch -d branch # Update remote list $ git remote update origin --prune # Checkout new branch $ git checkout -b new_branch origin/new_branch
by autosun
回應