[Git] Git command…

  • git stash: 暫存機制

    • git stash: 暫存機制,可保留目前狀態,重新取回其他版本的code

    • git stash clear: 清除所有暫存空間

    • git stash drop {git status name}: 清除指定的暫存狀態,如果不加參數,則刪除最近的那一個

    • git stash list: 查看目前暫存列表

    • git stash apply {git stash name}: 取回暫存的狀態,如果不加參數,則抓取最近的版本

    • git stash show {git stash name}: 查看指定的暫存與parent差異的部分,如果不加參數,則顯示最近的那一個版本

    • git stash branch {git stash name}: 將指定暫存轉為branch,同時暫存的部分會消失,如果不加參數,則轉換最近的版本

    • git stash save –keep-index: 將程式編輯時可能會產生的與遠端不相容的檔案先放置stash暫存中,之後再刪除,避免push至遠端

  • 忽略檔案設定

    編輯 .gitignore,假設要使git不追蹤 .jpg 檔案

    假設要使git不追蹤 .o 及 .a 的檔案

  • git status: 查詢目前狀態,位於哪個branch,暫存區的檔案資訊

  • git diff: 顯示未進入暫存區、commit 的目前詳細狀態,包含檔案中哪一行新增、刪除、修改等

    • git diff: –staged 顯示暫存區域及最後一次commit的差異

    • git diff {file_name}: 查詢檔案與所在branch中的原始檔案的差異資訊

  • git commit: 標註修改說明

    • git commit -m “說明文字”: 針對已加入準備push的暫存資料進行標註修改說明

    • git commit -a: 同上,會使用設定的預設文字編輯器跳出修改說明填寫畫面

  • git add 將檔案加入追蹤

    Ref: http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add/16162511#16162511

    version 1.x:
    git add .: 針對新增、修改的檔案修改追蹤狀態,刪除的不會更動
    git add [-A|–all]: 針對新增、修改、刪除的檔案修改追蹤狀態
    git add -u: 針對修改、刪除的檔案修改追蹤狀態,新增的檔案不會加入追蹤

    version 2.x:
    git add [-A|–all]: 針對新增、修改、刪除的檔案修改追蹤狀態
    git add .: 針對新增、修改、刪除的檔案修改追蹤狀態
    git add –ignore-removal .: 針對新增、修改的檔案修改追蹤狀態,刪除的不會更動
    git add -u: 針對修改、刪除的檔案修改追蹤狀態,新增的檔案不會加入追蹤

  • git log: 查詢 commit log

  • git blame {file name}: 查詢檔案每一行是由誰修改的

  • git rm {file_name}: 移除git中的檔案,同時一併刪除硬碟中的檔案

    • git rm -cached {file_name}: 移除已經add但還沒commit的檔案,即移除git index中的紀錄
  • git push: 將本地端的檔案push到預設的remote branch

    • git push origin branch_name: 將本地端的檔案push到遠端origin上的指定branch_name

    • git push origin :branch_name: 刪除遠端origin上指定的branch

    • git push origin –delete branch_name: 刪除遠端origin上指定的branch

    • git branch -d branch_name: 刪除本地的branch

  • git remote: 遠端操作

    • git remote: 顯示目前使用的遠端git_url (git://server_host/user_name/git_name)

    • git remote -v: 顯示所有的git_url資訊

    • git remote add remote_name git_url: 將遠端git_url設定別名成remote_name

    • git remote show origin: 查看遠端origin上的資料

    • git remote rename remote_name_old remote_name_new: 將遠端git server的別名進行更改

    • git remote rm remote_name: 刪除git server資訊

  • git pull: 將遠端的檔案拉回

    • git pull: 至預設的remote_name拉回檔案並merge進本地端正在進行的branch中

    • git pull local_branch_name remote_name/remote_branch_name: 至remote_name中拉回remote_branch_name並merge至本地的local_branch_name中

      git pull 動作完成後記得先使用git status檢視是否有衝突發生,若有,則需要先把衝突解決

    • git pull –rebase: 先把本地 repo. 從上次 pull 之後的變更暫存起來,回復到上次 pull 時的情況,接著套用遠端的變更,最後再套用剛暫存下來的本地變更

  • git branch: 分支

    • git branch: 查看本地端的branch清單

    • git branch -r: 查看遠端的branch清單

    • git branch new_local_branch_name: 從master複製一個新的本地端new_local_branch_name

    • git branch -d branch_name: 刪除本地端的branch_name

    • git branch –track local_branch_name remote_branch_name: 建立一個tracking遠端remote_branch_name的本地端local_branch_name,之後本地端的branch進行push或pull等動作將直接對應到遠端的branch

  • git checkout: 切換branch

    • git checkout local_branch_name: 切換至本地端指定的local_branch_name,若切換之前已有修改追蹤的檔案,務必先commit

    • git checkout -b new_local_branch_name: 以當前分支為根本,於本地端新建分支new_local_branch_name,並切換至new_local_branch_name (意即先 git branch new_local_branch_name 再 git checkout new_local_branch_name)

    • git checkout — file_name: 還原檔案file_name至於此branch中未修改的狀態

    • git checkout –: 將全部檔案還原至branch未修改的狀態

    • git checkout -f: 將全部檔案還原至branch未修改的狀態

  • git merge: 合併branch

    merge最重要的觀念是切換至主要的branch,再進行merge動作,意即先 git checkout master_branch,再git merge fixed_branch

    • git merge branch_name: 將branch_name套用至目前branch上
  • git reset: 回復

Ref. http://blog.miniasp.com/post/2013/08/19/Learning-Git-Part-1-Installation-Options-Tool-Usage-on-Local.aspx

Ref. http://kingofamani.gitbooks.io/git-teach/content/chapter_3_branch/chapter_3_branchmerge.html

  • git reset –hard [commit_id]: 回復至指定的commit_id版本,若沒指定,預設為HEAD,表示取消這次的merge,所有本地端的變更都會遺失

    • git reset –hard ORIG_HEAD: 回復到合併前的上一版,所有本地端的變更都會遺失

    • git reset –soft: 取消commit,檔案還在

    • git reset –mixed: 取消commit,取消add新增的檔案 <==預設動作

    • git reset commit_id: 回復至指定的commit_id版本,修改的檔案還在

    • git reset –soft HEAD^: 回復至上次commit後的動作

  • git clean -df: 清除暫存檔案,包含目錄

This entry was posted in Git. Bookmark the permalink.