Git 基礎

yanyin發表於2019-08-08

獲取 Git 倉庫

在已有目錄初始化倉庫

git init
git add *
git commit -m 'initial project version'

克隆已有倉庫

$ git clone https://github.com/libgit2/libgit2 [distination]

記錄每次更新到倉庫

檢查檔案狀態

$ git status 檢查當前檔案狀態

狀態類別:

  1. Untracted // 在之前提交的快照中不包含並且也不再暫存區的檔案
  2. Unmodified // 暫存並提交的檔案
  3. Modeified // 暫存並提交後又修改但未暫存的檔案
  4. Staged // 暫存但未提交的檔案

1 為未跟蹤的檔案, 2-4 為已跟蹤檔案。

狀態簡覽

$ git status -s
M  README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt
  • A: 新新增到暫存區中的檔案
  • 左M: 被修改並放到暫存區的檔案
  • 右M: 被修改但未放到暫存區的檔案
  • ??: 新新增的未跟蹤的檔案

跟蹤新檔案、暫存已修改檔案

$ git add filename

忽略檔案

在目錄下新建 .gitignore,檔案格式規範如下:

  • #開頭的行為註釋。
  • 匹配模式已 / 開頭防止遞迴,只忽略當前目錄下的檔案和目錄,不遞迴到子目錄。
  • 匹配模式已 / 結尾指定目錄。
  • 忽略指定模式以為的檔案或目錄,加 ! 取反。
  • 可以使用標準的 glob 模式匹配
    1. * 匹配零個或多個任意字元
    2. [abc] [0-9] 匹配其中一個字元
    3. ? 匹配任意一個字元
    4. 匹配任意中間目錄 a/**/z 匹配 a/z,a/b/z, a/b/c/z

檢視已暫存和未暫存的修改

$ git diff 檢視已暫存與未暫存檔案之間的差異

$ git diff --staged (--cacahed) 檢視已提交和已暫存檔案直接的差異

提交

$ git commit

$ git commit -m 'message'

$ git commit -a -m 'message' //直接暫存所有修改檔案並提交

移除檔案

$ git rm

刪除暫存和工作目錄下的檔案,要求工作目錄下的檔案在上一次暫存後無修改,但是在工作目錄手動刪除檔案後,可以通過該命令刪除暫存檔案,為了防止修改工作目錄下的檔案後沒有被跟蹤

$ gir rm -f filename

當已暫存的檔案有修改,強制刪除暫存和工作目錄下的檔案

$ git rm --cached filename

從暫存區移除檔案,但仍然儲存工作區目錄中的檔案。典型使用場景,當你忘記在 .gitignore 排除跟蹤某個檔案卻已經在暫存區中,現在你想讓檔案保留在磁碟中,卻不想讓 Git 繼續跟蹤。

提示:filename 可以用 file-glob 模式匹配

移動檔案

$ git mv file_from file_to

等價於

$ mv file_from file_to
$ git rm file_from
$ git add file_to

檢視提交歷史

$ git log

列出所有的更新,最近的更新在最上面

  • -p 或者 --patch 顯示每次提交的內容差異,-n 僅顯示最近 n 次提交。
  • --stat abbreviated stats 縮寫的統計顯示。
  • --shortstat 只顯示 --stat 中的更改、插入、刪除行
  • --preety=oneline,[short, full, fuller]
  • --preety=format:"%h %s"
    1. %H Commit hash
    2. %h Abbreviated commit hash
    3. %T Tree hash
    4. %t Abbreviated tree hash
    5. %P Parent hashes
    6. %p Abbreviated parent hashed
    7. %an Author name
    8. %ae Author email
    9. %ad Author date // 格式按照 --date=option 顯示
    10. %ar Author date //相對時間,即多久以前
    11. %cn Commiteer name
    12. %ce Comitter email
    13. %cd Committer date
    14. %cr Comitter date //相對時間
    15. Subject //提交資訊
  • --name-only 僅在提交資訊後顯示已修改的檔案清單
  • --name-status 顯示新增、修改、刪除的檔案清單
  • --abbrev-cimmit 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元
  • --relative-date 使用相對的顯示時間格式
  • --oneline --preety=oneline --abbrev-commit 簡寫
  • --since=2.weeks //提取兩週前
  • --author 指定作者
  • --grep 顯示提交資訊中包含字串的提交
    以上兩個可以同時用多個限定,直接是 any 的關係,加上 --all-match 為 and 關係。
  • -S 顯示增加或者刪除過某文字的提交
  • --no-merges 不顯示合併提交

撤銷操作

$ git commit --amend

用於上次提交忘記增加某一個檔案,或者修改上次提交資訊。

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

修改提交,將導致被修改的提交消失,只適用最後提交後有較小的改動。

取消檔案暫存

$ git reset HEAD <file>

取消暫存工作區中修改過的檔案(從最後一次提交中還原檔案到暫存區)

$ git reset -- <file>

遠端倉庫的使用

顯示遠端倉庫

$ git clone <src>

預設本地的遠端倉庫名為 origin

$git remote -v

顯示所有遠端倉庫

增加遠端倉庫

$ git remote add <loacl remote repository shortname> <src>

fetch 和 pull

$ git fetch [remote-name] 將資料拉取到你的本地倉庫,但不會自動合併或修改你當前的工作

git pull [remote-name] 拉取資料到本地倉庫,並自動合併到你目前的分支。

推送到遠端倉庫

$ git push <remote> <branch>

檢查遠端倉庫

$ git remote show origin <remote>

重新命名遠端倉庫

$ git remote rename <oldname> <newname>

刪除遠端倉庫

$ git remote rm(remove) <remote-name>

標籤

顯示標籤

$ git tag

顯示所有標籤

$ git tag -l(--list) 'v1.8.5*

顯示匹配萬用字元標籤名的標籤。 有萬用字元 -l 必有有。

新建標籤

附註標籤

$ git tag -a v1.4 -m "my version 1.4"

-a Annotated,即附註標籤,-m Message,標籤資訊

輕量標籤

$ git tag v1.4-lw

預設是打標籤物件是當前提交

對當前提交之前的提交打標籤

$ git tag -a <tagname> <commit-hash>

檢視某個標籤資訊

$ git show <tagname>

共享標籤

$ git push origin <tagname> //推送某個標籤到遠端倉庫
$ git push origin --tags //推送所有標籤到遠端倉庫

刪除標籤

$ git tag -d <tagname>

只會刪除本地標籤

$ git push origin :refs/tags/<tagname> //把一個空的標籤推送到遠端倉庫中的目標標籤
$ git push origin --delete <tagname>

檢出標籤

$ git checkout <tagname>

此時會使倉庫處於 "detatched HEAD" 狀態,因為檢出標籤,HEAD 指向這個標籤對應的提交,而不是標籤,不像檢出分支,HEAD會指向分支。當前所有的更改提交併不會反應在標籤上,而是新建一個提交。而新建標籤則可避免。

$ git checkout -b <new tagname> <old tagname

Git 別名

$ git config --global <alias> <command>

$ git config --global <alias> <!command>

當 command 不是 git 子命令,在命令前加 !

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章