天天看點

【硬核】工作第一天不會Git被辭退,200條Git指令大全

建立

建立一個新的 git 版本庫。這個版本庫的配置、存儲等資訊會被儲存到.git 檔案夾中、

# 初始化目前項目
$ git init

# 建立一個目錄,将其初始化為Git代碼庫
$ git init [project-name]

# 在指定目錄建立一個空的 Git 倉庫。運作這個指令會建立一個名為 directory,隻包含 .git 子目錄的空目錄。

$ git init --bare <directory>

# 下載下傳一個項目和它的整個代碼曆史
# 這個指令就是将一個版本庫拷貝到另一個目錄中,同時也将分支都拷貝到新的版本庫中。這樣就可以在新的版本庫中送出到遠端分支
$ git clone [url]      

配置

更改設定。可以是版本庫的設定,也可以是系統的或全局的

# 顯示目前的Git配置
$ git config --list

# 編輯Git配置檔案
$ git config -e [--global]

# 輸出、設定基本的全局變量
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "[email protected]"
$ git config --global user.name "My Name"

# 定義目前使用者所有送出使用的作者郵箱。
$ git config --global alias.<alias-name> <git-command>

# 為Git指令建立一個快捷方式(别名)。
$ git config --system core.editor <editor>      

幫助

git 内置了對指令非常詳細的解釋,可以供我們快速查閱

# 查找可用指令
$ git help

# 查找所有可用指令
$ git help -a

# 在文檔當中查找特定的指令
# git help <指令>
$ git help add
$ git help commit
$ git help init      

狀态

顯示索引檔案(也就是目前工作空間)和目前的頭指針指向的送出的不同

# 顯示分支,未跟蹤檔案,更改和其他不同
$ git status

# 檢視其他的git status的用法
$ git help status      

資訊

擷取某些檔案,某些分支,某次送出等 git 資訊

# 顯示commit曆史,以及每次commit發生變更的檔案
$ git log --stat

# 搜尋送出曆史,根據關鍵詞
$ git log -S [keyword]

# 顯示某個commit之後的所有變動,每個commit占據一行
$ git log [tag] HEAD --pretty=format:%s

# 顯示某個commit之後的所有變動,其"送出說明"必須符合搜尋條件
$ git log [tag] HEAD --grep feature

# 顯示某個檔案的版本曆史,包括檔案改名
$ git log --follow [file]
$ git whatchanged [file]

# 顯示指定檔案相關的每一次diff
$ git log -p [file]

# 顯示過去5次送出
$ git log -5 --pretty --oneline

# 顯示所有送出過的使用者,按送出次數排序
$ git shortlog -sn

# 顯示指定檔案是什麼人在什麼時間修改過
$ git blame [file]

# 顯示暫存區和工作區的差異
$ git diff

# 顯示暫存區和上一個commit的差異
$ git diff --cached [file]

# 顯示工作區與目前分支最新commit之間的差異
$ git diff HEAD

# 顯示兩次送出之間的差異
$ git diff [first-branch]...[second-branch]

# 顯示今天你寫了多少行代碼
$ git diff --shortstat "@{0 day ago}"

# 比較暫存區和版本庫差異
$ git diff --staged

# 比較暫存區和版本庫差異
$ git diff --cached

# 僅僅比較統計資訊
$ git diff --stat

# 顯示某次送出的中繼資料和内容變化
$ git show [commit]

# 顯示某次送出發生變化的檔案
$ git show --name-only [commit]

# 顯示某次送出時,某個檔案的内容
$ git show [commit]:[filename]

# 顯示目前分支的最近幾次送出
$ git reflog

# 檢視遠端分支
$ git br -r

# 建立新的分支
$ git br <new_branch>

# 檢視各個分支最後送出資訊
$ git br -v

# 檢視已經被合并到目前分支的分支
$ git br --merged

# 檢視尚未被合并到目前分支的分支
$ git br --no-merged      

添加

添加檔案到目前工作空間中。如果你不使用

git add

将檔案添加進去,那麼這些檔案也不會添加到之後的送出之中

# 添加一個檔案
$ git add test.js

# 添加一個子目錄中的檔案
$ git add /path/to/file/test.js

# 支援正規表達式
$ git add ./*.js

# 添加指定檔案到暫存區
$ git add [file1] [file2] ...

# 添加指定目錄到暫存區,包括子目錄
$ git add [dir]

# 添加目前目錄的所有檔案到暫存區
$ git add .

# 添加每個變化前,都會要求确認
# 對于同一個檔案的多處變化,可以實作分次送出
$ git add -p      

删除

rm 和上面的 add 指令相反,從工作空間中去掉某個檔案

# 移除 HelloWorld.js
$ git rm HelloWorld.js

# 移除子目錄中的檔案
$ git rm /pather/to/the/file/HelloWorld.js

# 删除工作區檔案,并且将這次删除放入暫存區
$ git rm [file1] [file2] ...

# 停止追蹤指定檔案,但該檔案會保留在工作區
$ git rm --cached [file]      

分支

管理分支,可以通過下列指令對分支進行增删改查切換等

# 檢視所有的分支和遠端分支
$ git branch -a

# 建立一個新的分支
$ git branch [branch-name]

# 重命名分支
# git branch -m <舊名稱> <新名稱>
$ git branch -m [branch-name] [new-branch-name]

# 編輯分支的介紹
$ git branch [branch-name] --edit-description

# 列出所有本地分支
$ git branch

# 列出所有遠端分支
$ git branch -r

# 建立一個分支,但依然停留在目前分支
$ git branch [branch-name]

# 建立一個分支,并切換到該分支
$ git checkout -b [branch]

# 建立一個分支,指向指定commit
$ git branch [branch] [commit]

# 建立一個分支,與指定的遠端分支建立追蹤關系
$ git branch --track [branch] [remote-branch]

# 切換到指定分支,并更新工作區
$ git checkout [branch-name]

# 切換到上一個分支
$ git checkout -

# 建立追蹤關系,在現有分支與指定的遠端分支之間
$ git branch --set-upstream [branch] [remote-branch]

# 合并指定分支到目前分支
$ git merge [branch]

# 選擇一個commit,合并進目前分支
$ git cherry-pick [commit]

# 删除分支
$ git branch -d [branch-name]

# 删除遠端分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

# 切換到某個分支
$ git co <branch>

# 建立新的分支,并且切換過去
$ git co -b <new_branch>

# 基于branch建立新的new_branch
$ git co -b <new_branch> <branch>

# 把某次曆史送出記錄checkout出來,但無分支資訊,切換到其他分支會自動删除
$ git co $id

# 把某次曆史送出記錄checkout出來,建立成一個分支
$ git co $id -b <new_branch>

# 删除某個分支
$ git br -d <branch>

# 強制删除某個分支 (未被合并的分支被删除的時候需要強制)
$ git br -D <branch>      

檢出

将目前工作空間更新到索引所辨別的或者某一特定的工作空間

# 檢出一個版本庫,預設将更新到master分支
$ git checkout
# 檢出到一個特定的分支
$ git checkout branchName
# 建立一個分支,并且切換過去,相當于"git branch <名字>; git checkout <名字>"
$ git checkout -b newBranch      

遠端同步

遠端同步的遠端分支

# 下載下傳遠端倉庫的所有變動
$ git fetch [remote]

# 顯示所有遠端倉庫
$ git remote -v

# 顯示某個遠端倉庫的資訊
$ git remote show [remote]

# 增加一個新的遠端倉庫,并命名
$ git remote add [shortname] [url]

# 檢視遠端伺服器位址和倉庫名稱
$ git remote -v

# 添加遠端倉庫位址
$ git remote add origin git@ github:xxx/xxx.git

# 設定遠端倉庫位址(用于修改遠端倉庫位址)
$ git remote set-url origin git@ github.com:xxx/xxx.git

# 删除遠端倉庫
$ git remote rm <repository>

# 上傳本地指定分支到遠端倉庫
# 把本地的分支更新到遠端origin的master分支上
# git push <遠端> <分支>
# git push 相當于 git push origin master
$ git push [remote] [branch]

# 強行推送目前分支到遠端倉庫,即使有沖突
$ git push [remote] --force

# 推送所有分支到遠端倉庫
$ git push [remote] --all      

撤銷

# 恢複暫存區的指定檔案到工作區
$ git checkout [file]

# 恢複某個commit的指定檔案到暫存區和工作區
$ git checkout [commit] [file]

# 恢複暫存區的所有檔案到工作區
$ git checkout .

# 重置暫存區的指定檔案,與上一次commit保持一緻,但工作區不變
$ git reset [file]

# 重置暫存區與工作區,與上一次commit保持一緻
$ git reset --hard

# 重置目前分支的指針為指定commit,同時重置暫存區,但工作區不變
$ git reset [commit]

# 重置目前分支的HEAD為指定commit,同時重置暫存區和工作區,與指定commit一緻
$ git reset --hard [commit]

# 重置目前HEAD為指定commit,但保持暫存區和工作區不變
$ git reset --keep [commit]

# 建立一個commit,用來撤銷指定commit
# 後者的所有變化都将被前者抵消,并且應用到目前分支
$ git revert [commit]

# 恢複最後一次送出的狀态
$ git revert HEAD

# 暫時将未送出的變化移除,稍後再移入
$ git stash
$ git stash pop

# 列所有stash
$ git stash list

# 恢複暫存的内容
$ git stash apply

# 删除暫存區
$ git stash drop      

commit

将目前索引的更改儲存為一個新的送出,這個送出包括使用者做出的更改與資訊

# 送出暫存區到倉庫區附帶送出資訊
$ git commit -m [message]

# 送出暫存區的指定檔案到倉庫區
$ git commit [file1] [file2] ... -m [message]

# 送出工作區自上次commit之後的變化,直接到倉庫區
$ git commit -a

# 送出時顯示所有diff資訊
$ git commit -v

# 使用一次新的commit,替代上一次送出
# 如果代碼沒有任何新變化,則用來改寫上一次commit的送出資訊
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定檔案的新變化
$ git commit --amend [file1] [file2] ...      

diff

顯示目前工作空間和送出的不同

# 顯示工作目錄和索引的不同
$ git diff

# 顯示索引和最近一次送出的不同
$ git diff --cached

# 顯示工作目錄和最近一次送出的不同
$ git diff HEAD      

grep

可以在版本庫中快速查找

可選配置:

# 感謝Travis Jeffery提供的以下用法:
# 在搜尋結果中顯示行号
$ git config --global grep.lineNumber true

# 是搜尋結果可讀性更好
$ git config --global alias.g "grep --break --heading --line-number"
# 在所有的java中查找variableName
$ git grep 'variableName' -- '*.java'

# 搜尋包含 "arrayListName" 和, "add" 或 "remove" 的所有行
$ git grep -e 'arrayListName' --and \( -e add -e remove \)      

log

顯示這個版本庫的所有送出

# 顯示所有送出
$ git log

# 顯示某幾條送出資訊
$ git log -n 10

# 僅顯示合并送出
$ git log --merges

# 檢視該檔案每次送出記錄
$ git log <file>

# 檢視每次詳細修改内容的diff
$ git log -p <file>

# 檢視最近兩次詳細修改内容的diff
$ git log -p -2

#檢視送出統計資訊
$ git log --stat      

merge

合并就是将外部的送出合并到自己的分支中

# 将其他分支合并到目前分支
$ git merge branchName

# 在合并時建立一個新的合并後的送出
# 不要 Fast-Foward 合并,這樣可以生成 merge 送出
$ git merge --no-ff branchName      

mv

重命名或移動一個檔案

# 重命名
$ git mv test.js test2.js

# 移動
$ git mv test.js ./new/path/test.js

# 改名檔案,并且将這個改名放入暫存區
$ git mv [file-original] [file-renamed]

# 強制重命名或移動
# 這個檔案已經存在,将要覆寫掉
$ git mv -f myFile existingFile      

tag

# 列出所有tag
$ git tag

# 建立一個tag在目前commit
$ git tag [tag]

# 建立一個tag在指定commit
$ git tag [tag] [commit]

# 删除本地tag
$ git tag -d [tag]

# 删除遠端tag
$ git push origin :refs/tags/[tagName]

# 檢視tag資訊
$ git show [tag]

# 送出指定tag
$ git push [remote] [tag]

# 送出所有tag
$ git push [remote] --tags

# 建立一個分支,指向某個tag
$ git checkout -b [branch] [tag]      

pull

從遠端版本庫合并到目前分支

# 從遠端origin的master分支更新版本庫
# git pull <遠端> <分支>
$ git pull origin master

# 抓取遠端倉庫所有分支更新并合并到本地,不要快進合并
$ git pull --no-ff      

ci

$ git ci <file>
$ git ci .
# 将git add, git rm和git ci等操作都合并在一起做
$ git ci -a
$ git ci -am "some comments"
# 修改最後一次送出記錄
$ git ci --amend
      

rebase (謹慎使用)

将一個分支上所有的送出曆史都應用到另一個分支上不要在一個已經公開的遠端分支上使用 rebase.

# 将experimentBranch應用到master上面
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch      

reset (謹慎使用)

将目前的頭指針複位到一個特定的狀态。這樣可以使你撤銷 merge、pull、commits、add 等 這是個很強大的指令,但是在使用時一定要清楚其所産生的後果

# 使 staging 區域恢複到上次送出時的狀态,不改變現在的工作目錄
$ git reset

# 使 staging 區域恢複到上次送出時的狀态,覆寫現在的工作目錄
$ git reset --hard

# 将目前分支恢複到某次送出,不改變現在的工作目錄
# 在工作目錄中所有的改變仍然存在
$ git reset dha78as

# 将目前分支恢複到某次送出,覆寫現在的工作目錄
# 并且删除所有未送出的改變和指定送出之後的所有送出
$ git reset --hard dha78as      

其他

# 生成一個可供釋出的壓縮包
$ git archive

# 打更新檔
$ git apply ../sync.patch

# 測試更新檔能否成功
$ git apply --check ../sync.patch

# 檢視Git的版本
$ git --version