2014年3月11日 星期二

Git 常用指令

1. git status
    可以看到目前 Git 的狀態
2. git add <filename>
    將檔案 filename 加入 staged
3. .gitignore 檔案讓你避免加入一些類型的檔案。格式大致上如下
# 註解,會被忽略。 # 不要追蹤檔名為 .a 結尾的檔案 *.a # 但是要追蹤 lib.a,即使上方已指定忽略所有的 .a 檔案 !lib.a # 只忽略根目錄下的 TODO 檔案。 不包含子目錄下的 TODO /TODO # 忽略build/目錄下所有檔案 build/ # 忽略doc/notes.txt但不包含doc/server/arch.txt doc/*.txt # ignore all .txt files in the doc/ directory doc/**/*.txt

4. git diff
命令比對目前工作目錄及暫存區域後告訴讀者哪些變更尚未被暫存。
5. git diff --staged
    這命令比對暫存區域及最後一個提交。
6. git commit -m "message" 或 git commit -m '可換行' 
7. git rm
    從已被追蹤檔案中移除並且提交
8. git log -p
    列出每次的更新之間差別的內容
9. git log --stat
    列出更新之間差別的內容的大綱
10. git commit --amend (會出現一個文書編輯器)
重新 commit ,例如想要修改上次 commit 或上次commit 漏掉了一些檔案沒 commit 可以用這個
11. git reset HEAD <filename>
將 filename 從 staged 中移除,取消暫存。
12. git checkout <PATH>
將 PATH 以下的檔案都 checkout。復原到最後一次 commit 的狀態。
13. git remote add [shortname] [url]
增加新的遠端儲存庫
14. git fetch [remote-name]
將所有本地端沒有的資料拉下來。僅僅將資料拉到本地端的儲存庫,但沒有合併,跟 git pull 不同。
15. git remote show [remote-name]
      看資訊可以看到remote branch, 跟 local branck track 的情形

16. git reset --hard 某個版本的 hash 編號   # 整個 Repository 恢復到某個版本的狀態

17. git branch 
       看 branch, 加 -d [branch name] 可以刪除 branch
18. git checkout -b [branch_name]
       直接開啟一個 branch_name 的分支並把 head 指向他。
19. git merge [branch_name]
       將 branch_name 分支和到目前 head 指向的分支。


20. git checkout --track origin/serverfix 建立一個 "tracking branch" for remote branch " origin/serverfix "

21. git branch -vv   觀察local branch 跟 remote branch 的關係

22. git reflog
      git reset [sha] 
      可以回復 git reset --hard

23. 查看 git repository 是從哪個地方 clone 的 url 位置
git config --get remote.origin.url

24. git rev-parse --verify HEAD
查看 HEAD 所指的 SHA-1 value

25. git push -u origin new_branch # 建立遠端 branch (將 new_branch 建立到遠端)