本備忘單總結了常用的 Git 命令行指令,以供快速參考。
創(chuàng)建一個新的本地存儲庫
$ git init [項目名稱]
克隆存儲庫(代碼倉庫)
$ git clone <git_url>
將存儲庫克隆到指定目錄
$ git clone <git_url> 指定目錄
將存儲庫克隆到指定目錄,并指定分支
$ git clone <git_url> -b <分支名稱> 指定目錄
在工作目錄中顯示修改后的文件,為您的下一次提交暫存
$ git status
暫存文件,準備提交
$ git add [file]
暫存所有更改的文件,準備提交
$ git add .
將所有暫存文件提交到版本化歷史記錄
$ git commit -m "commit message"
將所有跟蹤的文件提交到版本化歷史記錄
$ git commit -am "commit message"
取消暫存文件,保留文件更改
$ git reset [file]
將所有內容恢復到最后一次提交
$ git reset --hard
已更改但未暫存內容的差異
$ git diff
已 commited 但尚未提交的內容的差異
$ git diff --staged
在指定分支之前應用當前分支的任何提交
$ git rebase [branch]
設置將附加到您的提交和標簽的名稱
$ git config --global user.name "name"
設置將附加到您的提交和標簽 tags 的電子郵件地址
$ git config --global user.email "email"
啟用 Git 輸出的一些著色
$ git config --global color.ui auto
在文本編輯器中編輯全局配置文件
$ git config --global --edit
顯示本地 repo 配置設置
$ git config --list
刪除全局設置
$ git config --global --unset <entry-name>
列出所有本地分支
$ git branch
列出所有分支,本地和遠程
$ git branch -av
切換到 my_branch,并更新工作目錄
$ git checkout my_branch
創(chuàng)建一個名為 new_branch 的新分支
$ git checkout -b new_branch
刪除名為 my_branch 的分支
$ git branch -d my_branch
將分支 A 合并到分支 B
$ git checkout branchB
$ git merge branchA
標記當前提交
$ git tag my_tag
從遠程分支中創(chuàng)建并切換到本地分支
$ git checkout -b <branch-name> origin/<branch-name>
# 保存已修改和分階段的更改
$ git stash
# 列出隱藏文件更改的堆棧順序
$ git stash list
# 從存儲堆棧頂部編寫工作
$ git stash pop
# 丟棄存儲堆棧頂部的更改
$ git stash drop
# 回到某個 stash 的狀態(tài)
$ git stash apply <stash@{n}>
# 刪除所有的 stash
$ git stash clear
顯示當前活動分支的提交歷史
$ git log
顯示 branchA 上不在 branchB 上的提交
$ git log branchB..branchA
顯示更改文件的提交,即使跨重命名
$ git log --follow [file]
顯示 branchA 中的內容與 branchB 中的內容的差異
$ git diff branchB...branchA
以人類可讀的格式顯示 Git 中的任何對象
$ git show [SHA]
文件 .gitignore 指定了 Git 應該忽略的 未跟蹤的 文件
| :- | :- |
|---|---|
行首 # | 全行注釋,不支持行尾類注釋 (轉義 \#) |
行首 ! | 否定模式 (轉義 \!) |
** | 匹配任意路徑 |
* | 匹配任意多個字符 |
? | 匹配任意一個字符 |
doc/** | 匹配 doc 文件夾下的全部內容 |
doc/**/a | 匹配任意深度路徑下的 a 文件或文件夾 |
/ | 表示路徑分隔符,不區(qū)分操作系統(tǒng) |
/ 結尾 | 僅會匹配文件夾,否則會匹配文件和文件夾 |
| 空行 | 不匹配任何文件 |
| 行尾空格 | 默認被忽略,可使用\進行轉義 |
| 行首空格 | 被正常處理,不會被忽略 |
當前 .gitignore 文件定義規(guī)則的優(yōu)先級高于上級路徑 .gitignore 定義規(guī)則的優(yōu)先級;后定義的規(guī)則優(yōu)先級高于前面定義規(guī)則的優(yōu)先級
# 忽略當前目錄logs文件夾下的全部內容
/logs/
/logs/*
/logs/**
# 上述幾條規(guī)則等效
# 忽略 Mac 系統(tǒng)文件,包括任意子路徑下的同名文件(夾)
.DS_store
# 忽略 node_modules 文件夾,包括任意子路徑下的同名文件夾
node_modules/
# 忽略任意子路徑下build、target文件夾,
# 但不忽略src/main、src/test下的build、target文件夾
build/
!**/src/main/**/build/
!**/src/test/**/build/
target/
!**/src/main/**/target/
!**/src/test/**/target/
# 使用 ! 重新包含指定文件(夾)
!logs/.gitkeep
# 從工作目錄中刪除文件并暫存刪除
git rm <filename>
# 從版本控制中刪除文件但在本地保留文件
git rm --cached <filename>
# 更改文件名并準備提交
git mv <filename-orig> <filename-renamed>
從該 Git 遠程獲取所有分支
$ git fetch [alias]
將遠程分支合并到當前分支以使其保持最新狀態(tài)
$ git merge [alias]/[branch]
# 沒有快進
$ git merge --no-ff [alias]/[branch]
# 僅快進
$ git merge --ff-only [alias]/[branch]
將本地分支提交傳輸?shù)竭h程存儲庫分支
$ git push [alias] [branch]
從跟蹤遠程分支獲取并合并任何提交
$ git pull
將另一個分支的一個特定提交合并到當前分支
$ git cherry-pick [commit_id]
添加一個 git URL 作為別名
$ git remote add [alias] [url]
顯示您設置的遠程存儲庫的名稱
$ git remote
顯示遠程存儲庫的名稱和 URL
$ git remote -v
刪除遠程存儲庫
$ git remote rm [remote repo name]
更改 git repo 的 URL
$ git remote set-url origin [git_url]
從項目中刪除文件并暫存刪除以進行提交
$ git rm [file]
更改現(xiàn)有文件路徑并暫存移動
$ git mv [existing-path] [new-path]
顯示所有提交日志,并指示任何移動的路徑
$ git log --stat -M
$ cat ~/.ssh/config
Host gitlab.com
# 直接使用 sh**socks 提供的 socks5 代理端口
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
# 設置默認行為,以防人們沒有設置 core.autocrlf
* text=auto
# 明確聲明您希望始終規(guī)范化并在結帳時
# 轉換為本機行結尾的文本文件
*.c text
*.h text
# 聲明在結帳時始終以 CRLF 行結尾的文件
*.sln text eol=crlf
# 表示所有真正二進制且不應修改的文件
*.png binary
*.jpg binary
# 標記或取消標記要根據(jù)存儲庫的語言統(tǒng)計數(shù)據(jù)而
# 忽略或默認隱藏差異的路徑
search/index.json linguist-generated=true
# 以下屬性統(tǒng)計 SQL 文件
*.sql linguist-detectable=true
# 從統(tǒng)計信息中排除
docs/formatter.rb linguist-documentation=false
# 將它們從統(tǒng)計信息中排除
special-vendored-path/* linguist-vendored
# 將所有 .rb 文件檢測為 Java 文件
*.rb linguist-language=Java
重命名為new
$ git branch -m <new>
$ git branch -m <old> <new> #重命名分支
推送并重置
$ git push origin -u <new>
刪除遠程分支
$ git push origin --delete <old> #方法1
$ git push origin :oldBranchName #方法2
按內容搜索更改
$ git log -S'<a term in the source>'
顯示特定文件隨時間的變化
$ git log -p <file_name>
打印出很酷的日志可視化
$ git log --pretty=oneline --graph --decorate --all
列出所有分支及其上游
$ git branch -vv
快速切換到上一個分支
$ git checkout -
只獲取所有遠程分支
$ git branch -r
從另一個分支簽出單個文件
$ git checkout <branch> -- <file>
刪除本地存在遠程不存在的分支
git remote prune origin
$ git commit -v --amend
重寫最后的提交信息
git config core.fileMode false
不再將文件的權限變化視作改動
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
也可以看看:更多別名
# 查看git 的設置
$ git config --get core.ignorecase
# 設置大小寫敏感
$ git config core.ignorecase false
# 遠程有倆相同目錄,通過這種方式清除掉,然后提交記錄
$ git rm -r --cached <目錄/文件>
$ git rebase -i HEAD~3
# 表示要修改當前版本的倒數(shù)第三次狀態(tài)
# 將要更改的記錄行首單詞 pick 改為 edit
pick 96dc3f9 提交 commit 描述內容 1
pick f1cce8a 提交 commit 描述內容 2
pick 6293516 提交 commit 描述內容 3
# Rebase eeb03a4..6293516 onto eeb03a4
# (3 commands)
#
# Commands:
# p, pick = 使用提交
# r, reword = 使用提交,但編輯提交消息
# e, edit = 使用提交,但停止修改
# s, squash = 使用提交,但融合到先前的提交中
# f, fixup = 像 squash,但丟棄此提交的日志消息
# x, exec = 使用 shell 運行命令(該行的其余部分)
# d, drop = 刪除提交
保存并退出,會彈出下面提示
# 您現(xiàn)在可以修改提交,使用
#
# git commit --amend
#
# 對更改感到滿意后,運行
#
# git rebase --continue
#
# 1. 通過這條命令進入編輯更改 commit,保存退出
$ git commit --amend
# 2. 保存退出確認修改,繼續(xù)執(zhí)行下面命令,
$ git rebase --continue
# 如果修改多條記錄反復執(zhí)行上面兩條命令直到完成所有修改
# 最后,確保沒有人提交進行推送,最好不要加 -f 強制推送
$ git push -f origin master
# 撤銷一條記錄
$ git reset --hard HEAD~1
# 強制同步到遠程倉庫
$ git push -f origin HEAD:master
# 如果有的修改以及加入暫存區(qū)的話
$ git reset --hard
# 還原所有修改,不會刪除新增的文件
$ git checkout .
# 下面命令會刪除新增的文件
$ git clean -xdf
$ git rev-parse HEAD # e10721cb8859b2c
# 獲取短 hash
$ git rev-parse --short HEAD # e10721c
$ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
# 切換到 B 分支
$ git checkout <B>
# 將 A 分支 <hash-id> 的內容 pick 到 B 分支
$ git cherry-pick <hash-id>
$ git fetch --all && git reset --hard origin/master
拋棄本地所有的修改,回到遠程倉庫的狀態(tài)
$ git update-ref -d HEAD
把所有的改動都重新放回工作區(qū),并清空所有的 commit,這樣就可以重新提交第一個 commit 了
$ git diff --name-only --diff-filter=U
輸出工作區(qū)和暫存區(qū)的 different (不同)。
$ git diff
還可以展示本地倉庫中任意兩個 commit 之間的文件變動:
$ git diff <commit-id> <commit-id>
git diff --cached
$ git config --global core.quotepath false
$ git diff HEAD
輸出工作區(qū)、暫存區(qū) 和本地最近的版本(commit)的different(不同)。
$ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
$ git branch -u origin/mybranch
或者在 push 時加上 -u 參數(shù)
git push origin/mybranch -u
關聯(lián)之后,git branch -vv 就可以展示關聯(lián)的遠程分支名了, 同時推送到遠程倉庫直接:git push,不需要指定遠程倉庫
$ git remote show origin
$ git describe --tags --abbrev=0
$ git blame <file-name>
blame 的意思為責怪,你懂的。
$ git commit --amend --author='Author Name <email@address.com>'
$ git remote set-url origin <URL>
$ git remote add origin <remote-url>
$ git remote -v
$ git whatchanged --since='2 weeks ago'
$ git checkout <stash@{n}> -- <file-path>
$ git ls-files -t
$ git ls-files --others
$ git ls-files --others -i --exclude-standard
$ git bundle create <file> <branch-name>
$ git clone repo.bundle <repo-dir> -b <branch-name>
新建一個分支,分支內容就是上面 git bundle create 命令導出的內容
$ git rebase --autostash
$ git fetch origin pull/<id>/head:<branch-name>
$ git diff --word-diff
$ git clean -X -f
$ git status --ignored
$ git log Branch1 ^Branch2
$ git log --show-signature
$ git checkout --orphan <branch-name>
相當于保存修改,但是重寫 commit 歷史
$ git show <branch-name>:<file-name>
# 查看代理
$ git config --global http.proxy
$ git config --global https.proxy
$ git config --global socks.proxy
# 設置代理
# 適用于 privoxy 將 socks 協(xié)議轉為 http 協(xié)議的 http 端口
$ git config --global http.proxy http://127.0.0.1:1080
$ git config --global https.proxy http://127.0.0.1:1080
$ git config --global socks.proxy 127.0.0.1:1080
# 取消代理
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy
$ git config --global --unset socks.proxy
# 只對 github.com 設置代理
$ git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
$ git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
# 取消 github.com 代理
$ git config --global --unset http.https://github.com.proxy
$ git config --global --unset https.https://github.com.proxy
$ git clone --depth=1 https://github.com/user/repo.git
只會 clone 最近一次提交,將減少 clone 時間
關閉 track 指定文件的改動,也就是 Git 將不會在記錄這個文件的改動
git update-index --assume-unchanged path/to/file
恢復 track 指定文件的改動
git update-index --no-assume-unchanged path/to/file
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads
最新的放在最上面
git log --all --grep='<given-text>'
通過 grep 查找,given-text: 所需要查找的字段
git reset <file-name>
不添加參數(shù),默認是 -mixed
# 對于使用 git@ 協(xié)議的,可以配置 socks5 代理
# macOS 系統(tǒng)編輯 ~/.ssh/config 文件,添加這幾行,設置 github 代理
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
username 需要改成自己的git log --author="username" --pretty=tformat: --numstat | awk \
'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
git log --format='%aN' | sort -u |\
while read name; do echo -en "$name\t";\
git log --author="$name" --pretty=tformat: --numstat | awk \
'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
這里是排名前十,也可以更改排名
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 10
git log --oneline | wc -l
<type>(<scope>): <short summary>
│ │ │
│ │ └─? 緊湊簡短的描述,無需大寫,也不需要用句號結尾
│ │
│ └─? Commit 范圍: animations|bazel|benchpress|common|compiler|compiler-cli|core|
│ elements|forms|http|language-service|localize|platform-browser|
│ platform-browser-dynamic|platform-server|router|service-worker|
│ upgrade|zone.js|packaging|changelog|docs-infra|migrations|ngcc|ve|
│ devtools....
│
└─? Commit 類型: build|ci|doc|docs|feat|fix|perf|refactor|test
website|chore|style|type|revert
| 類型 | 描述 |
|---|---|
feat: | 新特性 |
fix(scope): | 修復 scope 中的 Bug |
feat!: / feat(scope)!: | breaking change / 重構 API |
chore(deps): | 更新依賴 |
| 類型 | 描述 |
|---|---|
build: | 變更影響的是構建系統(tǒng)或者外部依賴 (如: gulp, npm) |
ci: | 修改了 CI 配置文件或腳本 (如: Github Action, Travis) |
chore: | 【重要】 變更不影響源代碼或測試(如更新了輔助工具、庫等) |
docs: | 只修改了文檔 |
feat: | 【重要】 一個新特性 |
fix: | 【重要】 修復了一個 Bug |
perf: | 增強性能的代碼變更 |
refactor: | 并非修復 Bug 或添加新特性的代碼變更 |
revert: | 回退代碼 |
style: | 變更不影響一些有意義的代碼 (如: 刪除空格、格式化代碼、添加分號等) |
test: | 添加測試代碼或修正已有的測試 |
感谢您访问我们的网站,您可能还对以下资源感兴趣:
国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片