全網最全Git命令手冊
阿新 • • 發佈:2021-12-27
目錄
- 常用語句
- 新建
- 配置
- 狀態
- 資訊:獲取某些檔案,某些分支,某次提交等 git 資訊
- 新增
- 刪除
- 分支
- 檢出
- 遠端同步
- 撤銷:
- 提交
- diff: 顯示當前工作空間和提交的不同
- grep:可以在版本庫中快速查詢
- log: 顯示這個版本庫的所有提交
- merge: 合併就是將外部的提交合併到自己的分支中
- tag:
- push: 從遠端版本庫合併到當前分支
- rebase(謹慎使用)
- 其他:
- 參考文件:
常用語句
git init # 初始化本地git倉庫(建立新倉庫) git config --global user.name "xxx" # 配置使用者名稱 git config --global user.email "[email protected]" # 配置郵件 git config --global color.ui true # git status等命令自動著色 git config --global color.status auto git config --global color.diff auto git config --global color.branch auto git config --global color.interactive auto git config --global --unset http.proxy # remove proxy configuration on git git clone git+ssh://[email protected]/VT.git # clone遠端倉庫 git status # 檢視當前版本狀態(是否修改) git add xyz # 新增xyz檔案至index git add . # 增加當前子目錄下所有更改過的檔案至index git commit -m 'xxx' # 提交 git commit --amend -m 'xxx' # 合併上一次提交(用於反覆修改) git commit -am 'xxx' # 將add和commit合為一步 git rm xxx # 刪除index中的檔案 git rm -r * # 遞迴刪除 git log # 顯示提交日誌 git log -1 # 顯示1行日誌 -n為n行 git log -5 git log --stat # 顯示提交日誌及相關變動檔案 git log -p -m git show dfb02e6e4f2f7b573337763e5c0013802e392818 # 顯示某個提交的詳細內容 git show dfb02 # 可只用commitid的前幾位 git show HEAD # 顯示HEAD提交日誌 git show HEAD^# 顯示HEAD的父(上一個版本)的提交日誌 ^^為上兩個版本 ^5為上5個版本 git tag # 顯示已存在的tag git tag -a v2.0 -m 'xxx' # 增加v2.0的tag git show v2.0 # 顯示v2.0的日誌及詳細內容 git log v2.0 # 顯示v2.0的日誌 git diff # 顯示所有未新增至index的變更 git diff --cached # 顯示所有已新增index但還未commit的變更 git diff HEAD^ # 比較與上一個版本的差異 git diff HEAD -- ./lib # 比較與HEAD版本lib目錄的差異 git diff origin/master..master # 比較遠端分支master上有本地分支master上沒有的 git diff origin/master..master --stat # 只顯示差異的檔案,不顯示具體內容 git remote add origin git+ssh://[email protected]/VT.git # 增加遠端定義(用於push/pull/fetch) git branch # 顯示本地分支 git branch --contains 50089 # 顯示包含提交50089的分支 git branch -a # 顯示所有分支 git branch -r # 顯示所有原創分支 git branch --merged # 顯示所有已合併到當前分支的分支 git branch --no-merged # 顯示所有未合併到當前分支的分支 git branch -m master master_copy # 本地分支改名 git checkout -b master_copy # 從當前分支建立新分支master_copy並檢出 git checkout -b master master_copy # 上面的完整版 git checkout features/performance # 檢出已存在的features/performance分支 git checkout --track hotfixes/BJVEP933 # 檢出遠端分支hotfixes/BJVEP933並建立本地跟蹤分支 git checkout v2.0 # 檢出版本v2.0 git checkout -b devel origin/develop # 從遠端分支develop建立新本地分支devel並檢出 git checkout -- README # 檢出head版本的README檔案(可用於修改錯誤回退) git merge origin/master # 合併遠端master分支至當前分支 git cherry-pick ff44785404a8e # 合併提交ff44785404a8e的修改 git push origin master # 將當前分支push到遠端master分支 git push origin :hotfixes/BJVEP933 # 刪除遠端倉庫的hotfixes/BJVEP933分支 git push --tags # 把所有tag推送到遠端倉庫 git fetch # 獲取所有遠端分支(不更新本地分支,另需merge) git fetch --prune # 獲取所有原創分支並清除伺服器上已刪掉的分支 git pull origin master # 獲取遠端分支master並merge到當前分支 git mv README README2 # 重新命名檔案README為README2 git reset --hard HEAD # 將當前版本重置為HEAD(通常用於merge失敗回退) git rebase git branch -d hotfixes/BJVEP933 # 刪除分支hotfixes/BJVEP933(本分支修改已合併到其他分支) git branch -D hotfixes/BJVEP933 # 強制刪除分支hotfixes/BJVEP933 git ls-files # 列出git index包含的檔案 git show-branch # 圖示當前分支歷史 git show-branch --all # 圖示所有分支歷史 git whatchanged # 顯示提交歷史對應的檔案修改 git revert dfb02e6e4f2f7b573337763e5c0013802e392818 # 撤銷提交dfb02e6e4f2f7b573337763e5c0013802e392818 git ls-tree HEAD # 內部命令:顯示某個git物件 git rev-parse v2.0 # 內部命令:顯示某個ref對於的SHA1 HASH git reflog # 顯示所有提交,包括孤立節點 git show HEAD@{5} git show master@{yesterday} # 顯示master分支昨天的狀態 git log --pretty=format:'%h %s' --graph # 圖示提交日誌 git show HEAD~3 git show -s --pretty=raw 2be7fcb476 git stash # 暫存當前修改,將所有至為HEAD狀態 git stash list # 檢視所有暫存 git stash show -p stash@{0} # 參考第一次暫存 git stash apply stash@{0} # 應用第一次暫存 git grep "delete from" # 檔案中搜索文字“delete from” git grep -e '#define' --and -e SORT_DIRENT git gc git fsck
新建
建立一個新的 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 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. # 新增一個子目錄中的檔案 $ 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@ :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" # 在所有的中查詢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] [commiwww.cppcns.comt] # 刪除本地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]
push: 從遠端版本庫合併到當前分支
# 從遠端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
參考文件:
常用 Git 命令清單: https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
、、Nginx、、Git、SEcVzohnchell 等筆記: https://github.com/guanguans/notes
到此這篇關於全網最全Git命令手冊的文章就介紹到這了,更多相關全網最全Git命令手冊內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!