Git——常用命令總結
阿新 • • 發佈:2020-07-26
前言
記錄日常中常用的Git命令
安裝
Git官網
| Git官網
Git下載
| Git下載
配置
配置user.name
和user.email
git config --global user.name 'your_name'
git cofig --global user.email '[email protected]'
# 將預設編輯器設定為vim
git config --global core.editor vim
作用域
# 某個倉庫有效(預設) git config --local # 全域性有效(當前使用者所有倉庫有效) git config --global # 系統所有登陸使用者有效 git config --system
顯示config配置
# 檢視本地配置
git config --list --local
# 檢視全域性配置
git config --list --global
# 檢視系統配置
git config --list --system
建立倉庫
cd your_project git init git config --global user.name 'your_name' git cofig --global user.email '[email protected]' git remote add origin <git addr> git add -A git commit -m "add file" git push
目錄結構
.git ├─ COMMIT_EDITMSG ├─ FETCH_HEAD ├─ HEAD #該檔案表示當前本地簽出的分支,例如儲存的值:ref: refs/heads/master ├─ ORIG_HEAD ├─ config ├─ description ├─ hooks │ ├─ applypatch-msg.sample │ ├─ commit-msg.sample │ ├─ fsmonitor-watchman.sample │ ├─ post-update.sample │ ├─ pre-applypatch.sample │ ├─ pre-commit.sample │ ├─ pre-push.sample │ ├─ pre-rebase.sample │ ├─ pre-receive.sample │ ├─ prepare-commit-msg.sample │ └─ update.sample ├─ index #儲存緩衝區(GitExtensions中的stage)的內容,內容包括它指向的檔案的時間戳、檔名、sha1值等;(git三大區域:工作區,暫存區,歷史記錄區) ├─ info │ ├─ exclude │ └─ refs ├─ logs │ ├─ HEAD │ └─ refs │ ├─ heads │ ├─ remotes │ └─ stash ├─ objects #儲存物件的目錄,本地倉庫,git中物件分為三種:commit物件,tree物件(多叉樹),blob物件 │ ├─ 06 │ │ └─ abbb19400a205bdbd1dc0a28b986323fc8b808 │ ├─ 0e │ │ └─ 5d9122f24db7db88b7688759bf457be74e8643 │ ├─ 16 │ │ └─ 89c24264c1fde08df4773db3cfbf6316d7e2b3 │ ├─ 2c │ │ └─ 936a4ca60dd5c8f4ceeb9fede83a2c2f2c69b5 │ ├─ 48 │ │ └─ 0be438bab34a5361d8949f690a346973b8eb79 │ ├─ 72 │ │ └─ 7c5abd67dfeb49ee214b6a112445fbb550d3c5 │ ├─ bd │ │ └─ 3441fa796371e5f796a1d522f8052b763ad533 │ ├─ f1 │ │ └─ 894b05a9021dded510f71fef2be2cfb9864db6 │ ├─ info │ │ └─ packs │ └─ pack │ ├─ pack-76de1ecd79b2e31879a027b482c07377610a8bd6.idx │ └─ pack-76de1ecd79b2e31879a027b482c07377610a8bd6.pack ├─ packed-refs └─ refs #儲存指向branch的最近一次commit物件的指標,也就是commit物件的sha-1值(就是hash值,sha-1是一種雜湊演算法),refs的目錄下包括以下目錄(git init後並沒有remotes和stash,需要有從remote地址中pull code等互動性操作才會出現remotes目錄,stash檔案則是做了stash操作才會出現): ├─ heads │ ├─ dev │ ├─ master │ └─ tb-customer ├─ remotes │ └─ origin └─ stash
命令詳解
add
# 將工作空間修改和刪除的檔案新增到暫存區
git add -u
# 將工作空間新增和被修改的檔案新增的暫存區
git add .
# 將工作空間新增、修改和刪除的檔案新增到暫存區
git add -A
mv
# 修改檔名稱
git mv <old_name> <new_name>
rm
# 刪除檔案
git rm <file_name>
log
# 檢視單行的簡潔日誌
git log --oneline
# 檢視最近2條簡潔日誌
git log --oneline -n4
git log --oneline -4
# 檢視所有分支的歷史日誌
git log --all
# 以圖形化展示歷史日誌
git log --all --graph
# 以圖形化展示所有分支的最近4條簡潔日誌
git log --all --oneline -4 --graph
# 在網頁端檢視git log命令文件
git help --web log
# 在GUI介面檢視
gitk --all
commit
# 修改上一次提交commit資訊
git commit --amend
# 修改指定commit資訊
git rebase -i <commit_id>
r <修改commit資訊>
p <保留commit資訊>
# 把連續多個commit合併成一個
git rebase -i <last_commit_id>
p <需保留的cmmit資訊>
s <需合併到上一個commit資訊>
# 把間隔的commit合併成一個
git rebase -i <last_commit_id>
p <需保留的commit資訊>
s <把需要合併的commit的移動到需要保留的commit下面並選擇squash策略>
rebase策略
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
diff
# 比較暫存區和HEAD所含檔案差異
git diff --cached
git diff --staged
# 比較工作區和暫存區所含檔案差異
git diff
# 比較工作去和暫存區指定檔案差異(可比較多個檔案)
git diff -- <file_name_1> <file_name_2>
# 比較兩個commit之間的差別
git diff <commit_id_1> <commit_id_2>
# 比較兩個commit之間的指定檔案差別
git diff <commit_id_1> <commit_id_2> -- <file_name>
reset
# 將暫存區恢復成和HEAD一樣
git reset HEAD
# 將暫存區指定檔案恢復成和HEAD一樣(可多個檔案)
git reset HEAD -- <file_name_1> <file_name_2>
# 將暫存區恢復到指定的commit
git reset --hard <commit_id>
# 將指定檔案的工作區恢復成和暫存區一樣
git checkout -- <file_name>
git restore -- <file_name>
branch
# 檢視本地所有分支
git branch -v
# 刪除指定分支
git branch -d <branch_name>
# 強制刪除指定分支
git branch -D <branch_name>
checkout
# 建立新的分支
git checkout -b <branch_name>
# 切換分支
git checkout <branch_name>
git switch <branch_name>
stash
# 儲存當前修改
git stash
# 檢視stash列表
git stash list
# 恢復之前儲存內容(保留stash資訊)
git stash apply
# 恢復之前儲存內容(刪除stash資訊)
git stash pop
remote
# 新增遠端分支(一般使用智慧協議[http/https|ssh])
git remote add <local_barnch_name> <remote_address>
# 檢視本地分支
git remote -v
# 檢視所有分支
git remote -va
fetch
# 拉取遠端所有分支
git fetch --all
# 拉取指定分支
git fetch <local_branch_name> <remote_branch_name>
#
merge
# 合併和遠端不相關的分支
git merge --allow-unrelated-histories <remote_branch_name>
# 圖形化合並介面
git mergetool