Git介紹

前言

Git,是一個開源的分佈式版本控制系統,用以有效、高速的處理從很小到非常大的項目版本管理。支持克隆/下載。

介於Git已廣爲人知,本文不再詳細描述Git使用教程,只介紹一些工具或學習資料。

Git教程: Git Book 官方中文文檔

Git GUI客戶端: TortoiseGit(Windows)

Git常用命令

倉庫

  1. # 在當前目錄新建一個Git代碼庫
  2. $ git init
  3. # 新建一個目錄,將其初始化爲Git代碼庫
  4. $ git init [project-name]
  5. # 下載一個項目和它的整個代碼歷史
  6. $ git clone [url]

配置

  1. # 顯示當前的Git配置
  2. $ git config --list
  3. # 編輯Git配置文件
  4. $ git config -e [--global]
  5. # 設置提交代碼時的用戶信息
  6. $ git config [--global] user.name "[name]"
  7. $ git config [--global] user.email "[email address]"

增加/刪除文件

  1. # 添加指定文件到暫存區
  2. $ git add [file1] [file2] ...
  3. # 添加指定目錄到暫存區,包括子目錄
  4. $ git add [dir]
  5. # 添加當前目錄的所有文件到暫存區
  6. $ git add .
  7. # 添加每個變化前,都會要求確認
  8. # 對於同一個文件的多處變化,可以實現分次提交
  9. $ git add -p
  10. # 刪除工作區文件,並且將這次刪除放入暫存區
  11. $ git rm [file1] [file2] ...
  12. # 停止追蹤指定文件,但該文件會保留在工作區
  13. $ git rm --cached [file]
  14. # 改名文件,並且將這個改名放入暫存區
  15. $ git mv [file-original] [file-renamed]

代碼提交

  1. # 提交暫存區到倉庫區
  2. $ git commit -m [message]
  3. # 提交暫存區的指定文件到倉庫區
  4. $ git commit [file1] [file2] ... -m [message]
  5. # 提交工作區自上次commit之後的變化,直接到倉庫區
  6. $ git commit -a
  7. # 提交時顯示所有diff信息
  8. $ git commit -v
  9. # 使用一次新的commit,替代上一次提交
  10. # 如果代碼沒有任何新變化,則用來改寫上一次commit的提交信息
  11. $ git commit --amend -m [message]
  12. # 重做上一次commit,幷包括指定文件的新變化
  13. $ git commit --amend [file1] [file2] ...

分支

  1. # 列出所有本地分支
  2. $ git branch
  3. # 列出所有遠程分支
  4. $ git branch -r
  5. # 列出所有本地分支和遠程分支
  6. $ git branch -a
  7. # 新建一個分支,但依然停留在當前分支
  8. $ git branch [branch-name]
  9. # 新建一個分支,並切換到該分支
  10. $ git checkout -b [branch]
  11. # 新建一個分支,指向指定commit
  12. $ git branch [branch] [commit]
  13. # 新建一個分支,與指定的遠程分支建立追蹤關係
  14. $ git branch --track [branch] [remote-branch]
  15. # 切換到指定分支,並更新工作區
  16. $ git checkout [branch-name]
  17. # 切換到上一個分支
  18. $ git checkout -
  19. # 建立追蹤關係,在現有分支與指定的遠程分支之間
  20. $ git branch --set-upstream [branch] [remote-branch]
  21. # 合併指定分支到當前分支
  22. $ git merge [branch]
  23. # 選擇一個commit,合併進當前分支
  24. $ git cherry-pick [commit]
  25. # 刪除分支
  26. $ git branch -d [branch-name]
  27. # 刪除遠程分支
  28. $ git push origin --delete [branch-name]
  29. $ git branch -dr [remote/branch]

標籤

  1. # 列出所有tag
  2. $ git tag
  3. # 新建一個tag在當前commit
  4. $ git tag [tag]
  5. # 新建一個tag在指定commit
  6. $ git tag [tag] [commit]
  7. # 刪除本地tag
  8. $ git tag -d [tag]
  9. # 刪除遠程tag
  10. $ git push origin :refs/tags/[tagName]
  11. # 查看tag信息
  12. $ git show [tag]
  13. # 提交指定tag
  14. $ git push [remote] [tag]
  15. # 提交所有tag
  16. $ git push [remote] --tags
  17. # 新建一個分支,指向某個tag
  18. $ git checkout -b [branch] [tag]

查看信息

  1. # 顯示有變更的文件
  2. $ git status
  3. # 顯示當前分支的版本歷史
  4. $ git log
  5. # 顯示commit歷史,以及每次commit發生變更的文件
  6. $ git log --stat
  7. # 搜索提交歷史,根據關鍵詞
  8. $ git log -S [keyword]
  9. # 顯示某個commit之後的所有變動,每個commit佔據一行
  10. $ git log [tag] HEAD --pretty=format:%s
  11. # 顯示某個commit之後的所有變動,其"提交說明"必須符合搜索條件
  12. $ git log [tag] HEAD --grep feature
  13. # 顯示某個文件的版本歷史,包括文件改名
  14. $ git log --follow [file]
  15. $ git whatchanged [file]
  16. # 顯示指定文件相關的每一次diff
  17. $ git log -p [file]
  18. # 顯示過去5次提交
  19. $ git log -5 --pretty --oneline
  20. # 顯示所有提交過的用戶,按提交次數排序
  21. $ git shortlog -sn
  22. # 顯示指定文件是什麼人在什麼時間修改過
  23. $ git blame [file]
  24. # 顯示暫存區和工作區的差異
  25. $ git diff
  26. # 顯示暫存區和上一個commit的差異
  27. $ git diff --cached [file]
  28. # 顯示工作區與當前分支最新commit之間的差異
  29. $ git diff HEAD
  30. # 顯示兩次提交之間的差異
  31. $ git diff [first-branch]...[second-branch]
  32. # 顯示今天你寫了多少行代碼
  33. $ git diff --shortstat "@{0 day ago}"
  34. # 顯示某次提交的元數據和內容變化
  35. $ git show [commit]
  36. # 顯示某次提交發生變化的文件
  37. $ git show --name-only [commit]
  38. # 顯示某次提交時,某個文件的內容
  39. $ git show [commit]:[filename]
  40. # 顯示當前分支的最近幾次提交
  41. $ git reflog

遠程同步

  1. # 下載遠程倉庫的所有變動
  2. $ git fetch [remote]
  3. # 顯示所有遠程倉庫
  4. $ git remote -v
  5. # 顯示某個遠程倉庫的信息
  6. $ git remote show [remote]
  7. # 增加一個新的遠程倉庫,並命名
  8. $ git remote add [shortname] [url]
  9. # 取回遠程倉庫的變化,並與本地分支合併
  10. $ git pull [remote] [branch]
  11. # 上傳本地指定分支到遠程倉庫
  12. $ git push [remote] [branch]
  13. # 強行推送當前分支到遠程倉庫,即使有衝突
  14. $ git push [remote] --force
  15. # 推送所有分支到遠程倉庫
  16. $ git push [remote] --all

撤銷

  1. # 恢復暫存區的指定文件到工作區
  2. $ git checkout [file]
  3. # 恢復某個commit的指定文件到暫存區和工作區
  4. $ git checkout [commit] [file]
  5. # 恢復暫存區的所有文件到工作區
  6. $ git checkout .
  7. # 重置暫存區的指定文件,與上一次commit保持一致,但工作區不變
  8. $ git reset [file]
  9. # 重置暫存區與工作區,與上一次commit保持一致
  10. $ git reset --hard
  11. # 重置當前分支的指針爲指定commit,同時重置暫存區,但工作區不變
  12. $ git reset [commit]
  13. # 重置當前分支的HEAD爲指定commit,同時重置暫存區和工作區,與指定commit一致
  14. $ git reset --hard [commit]
  15. # 重置當前HEAD爲指定commit,但保持暫存區和工作區不變
  16. $ git reset --keep [commit]
  17. # 新建一個commit,用來撤銷指定commit
  18. # 後者的所有變化都將被前者抵消,並且應用到當前分支
  19. $ git revert [commit]
  20. 暫時將未提交的變化移除,稍後再移入
  21. $ git stash
  22. $ git stash pop

其他

  1. # 生成一個可供發佈的壓縮包
  2. $ git archive