一. 輸出漂亮的log --graph
使用git log --graph -2
或git log --pretty=format:"%h"
可以定制很多的輸出格式,在此基礎(chǔ)上添加自己喜歡樣式,并保存到git config中下次使用就免去了每次輸入一長串命令的困擾
- 全局添加
git config --global alias.lg "log --graph"
或者使用更漂亮的
- 使用年月日時(shí)間具體日期時(shí)間格式
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"
- 使用相對時(shí)間日期格式
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
效果如下:
- 使用可讀日期時(shí)間
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad)%Creset%Cblue(%an)%Creset' --abbrev-commit --date=format:'%Y-%m-%d %H:%M'"
效果如下:
tip:如果輸出太長,別忘了按q退出. 如果想只顯示前幾行l(wèi)og,可加參數(shù) 如:git lg -10
,會輸出10條數(shù)據(jù)
二. 查看歷史記錄更改內(nèi)容
git log比較有用的選項(xiàng)是 -p 或 --patch ,它會顯示每次提交所引入的差異(按 補(bǔ)丁 的格式輸出)被冒。 你也可以限制顯示的日志條目數(shù)量糙捺,例如使用 -2 選項(xiàng)來只顯示最近的兩次提交:
git log -p -2
三. 查看歷史提交簡略統(tǒng)計(jì)信息
git log --stat
四. git log 常用選項(xiàng)
選項(xiàng) | 說明 |
---|---|
-p | 按補(bǔ)丁格式顯示每個(gè)提交引入的差異尘应。 |
--stat | 顯示每次提交的文件修改統(tǒng)計(jì)信息箍邮。 |
--shortstat | 只顯示 --stat 中最后的行數(shù)修改添加移除統(tǒng)計(jì)坝锰。 |
--name-only | 僅在提交信息后顯示已修改的文件清單处窥。 |
--name-status | 顯示新增、修改、刪除的文件清單障贸。 |
--abbrev-commit | 僅顯示 SHA-1 校驗(yàn)和所有 40 個(gè)字符中的前幾個(gè)字符。 |
--relative-date | 使用較短的相對時(shí)間而不是完整格式顯示日期(比如“2 weeks ago”)峻仇。 |
--graph | 在日志旁以 ASCII 圖形顯示分支與合并歷史。 |
--pretty | 使用其他格式顯示歷史提交信息邑商∩闩兀可用的選項(xiàng)包括 oneline、short人断、full吭从、fuller 和 format(用來定義自己的格式)。 |
--oneline | --pretty=oneline --abbrev-commit 合用的簡寫恶迈。 |
-<n> | 僅顯示最近的 n 條提交涩金。 |
--since, --after | 僅顯示指定時(shí)間之后的提交。 |
--until, --before | 僅顯示指定時(shí)間之前的提交暇仲。 |
--author | 僅顯示作者匹配指定字符串的提交步做。 |
--committer | 僅顯示提交者匹配指定字符串的提交。 |
--grep | 僅顯示提交說明中包含指定字符串的提交奈附。 |
-S | 僅顯示添加或刪除內(nèi)容匹配指定字符串的提交全度。 |
示例:
git log --pretty="%h - %s" --author='Junio C Hamano' --since="2008-10-01" \
--before="2008-11-01" --no-merges -- t/
五. git清除所有修改
- 首先查看git狀態(tài),是否有add或commit
git status
2.在未發(fā)生任何add或commit的情況下:
git checkout .
這條命令,只能清除所有修改的文件,但是新建的文件和文件夾無法清除,還必須使用:
git clean -df
清除所有新建的文件及文件夾
- 對于add的部分,先要撤銷add:
撤銷所有已a(bǔ)dd文件git reset .
或撤銷特定文件git reset -- a.txt b.txt
然后再進(jìn)行第一步的操作即可
六. git查看相關(guān)配置
$ git config
Config file location
--global use global config file
--system use system config file
--local use repository config file
--worktree use per-worktree config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object
Action
--get get value: name [value-regex]
--get-all get all values: key [value-regex]
--get-regexp get values for regexp: name-regex [value-regex]
--get-urlmatch get value specific for the URL: section[.var] URL
--replace-all replace all matching variables: name value [value_regex]
--add add a new variable: name value
--unset remove a variable: name [value-regex]
--unset-all remove all matches: name [value-regex]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
-e, --edit open an editor
--get-color find the color configured: slot [default]
--get-colorbool find the color setting: slot [stdout-is-tty]
Type
-t, --type <> value is given this type
--bool value is "true" or "false"
--int value is decimal number
--bool-or-int value is --bool or --int
--path value is a path (file or directory name)
--expiry-date value is an expiry date
Other
-z, --null terminate values with NUL byte
--name-only show variable names only
--includes respect include directives on lookup
--show-origin show origin of config (file, standard input, blob, command line)
--default <value> with --get, use default value when missing entry
使用方式:
git config + [Config file localtion] + [Action]
, 示例如下:
$ git config --system --list
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
credential.helper=manager
...
$ git config --local --list
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=https://gitee.com/.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
gitflow.branch.master=master
gitflow.branch.develop=develop
branch.develop.remote=origin
branch.develop.merge=refs/heads/develop
gitflow.prefix.feature=
gitflow.prefix.bugfix=
gitflow.prefix.release=
gitflow.prefix.hotfix=
gitflow.prefix.support=
gitflow.prefix.versiontag=
gitflow.path.hooks=C:/work/web/.git/hooks
七. 新添加的文件被忽略問題
- 經(jīng)過分析,是因?yàn)樵?code>.gitignore文件里面將此文件的父文件夾給添加進(jìn)去了,即忽略了該文件夾下的所有文件改動,所以就不會上傳到遠(yuǎn)端.
解決方法:將.gitignore
文件里的規(guī)則改變一下,刪除該文件夾路徑規(guī)則(或修改),使此文件能夠在git
控制之下. - 還有一種情況是,git默認(rèn)忽略.dll .exe文件,需要手動添加
!*.dll
和!*.exe
八. 拉取代碼: git pull , git merge ,git stash
- 將修改好的代碼提交
git commit -am "commit something"
將代碼從遠(yuǎn)端拉回
方法1:git pull
方法2:git fetch origin
+git merge origin/master
想臨時(shí)不提交,并把代碼拉到本地
git stash
git pull
git stash pop
九. 查看修改的內(nèi)容
- 查看未暫存的修改
git diff
orgit diff HEAD
- 查看已暫存修改
git diff --cached
orgit diff --staged
十. 統(tǒng)計(jì)相關(guān)
1. 統(tǒng)計(jì)git提交次數(shù), 展示所有人的提交次數(shù)
. 注意, 統(tǒng)計(jì)相關(guān)命令運(yùn)用到了bash命令, 需要在bash中運(yùn)行
$ git log | grep "^Author: " | awk '{print $2}' | sort | uniq -c | sort -k1,1nr
效果如下:
2. 統(tǒng)計(jì)時(shí)間內(nèi)提交次數(shù)
$ git log --author=換成你的git名字 --since="2020-1-29" --no-merges | grep -e 'commit [a-zA-Z0-9]*' | wc -l
效果如下:
3. 統(tǒng)計(jì)提交行數(shù)
將展示該用戶增加行數(shù),刪減行數(shù)斥滤,剩余行數(shù)
git log --author="換成你的git名字" --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 }' -
效果如下:
十一. 修改已提交commit信息
1. 修改最近依次commit的信息
使用命令: git commit --amend
此時(shí)會啟動安裝git時(shí)的默認(rèn)編輯器, 如果默認(rèn)時(shí)vi則使用vi相關(guān)命令修改, 如果時(shí)其他文本編輯器(如: vscode)修改會更簡單(不需要記住vi命令).
如果是vi編輯器, 記住幾點(diǎn)就可以: 按i進(jìn)入編輯模式, 開始編輯要修改的內(nèi)容, 改完后按ESC鍵退出編輯模式,然后按:wq
保存我們編輯的信息.完成.
無論用那種編輯器修改完保存之后會看到最后的一條git提交信息已經(jīng)修改了.
2. 修改最近的歷史記錄提交信息
例如我提交了幾條信息:
這里我打算修改倒數(shù)第三條, feat: 能源相關(guān)測試
,
則使用命令:git rebase -i HEAD~3
, 此時(shí)如果用的默認(rèn)編輯器是vscode, 則會自動代開, 顯示如下
選擇對應(yīng)的commit將其改成edit后保存后
在cmd里會提示使用git commit --amend
修改之后使用git rebase --continue
在cmd里輸入
git commit --amend
修改之后保存,然后輸入命令
git rebase --continue
git log
后查看修改的commit已經(jīng)修改成功.
十三. 撤回最近提交的commit
- 修改的內(nèi)容不會丟失
git reset HEAD~
git reset HEAD~2 //數(shù)字是撤銷前N次
- 修改的內(nèi)容會丟失
git reset --hard HEAD^1
十二. 修改log中日期格式
默認(rèn)格式如下:
Date: Mon Sep 28 09:29:45 2020 +0800
對英文不是很好且對時(shí)區(qū)沒有很強(qiáng)概念的人來說比較晦澀難懂
方法: 修改config格式化日期
git config --global log.date format:'%Y-%m-%d %H:%M:%S'
效果:
Date: 2020-09-28 09:29:45
十三. 對git config的查看修改刪除等操作說明
如果對git config命令不是很熟悉,
- 查看自帶的幫助
git config --global --help
顯示如下:
PS D:\work\web\els-opt\els-opt_2> git config --global --help
usage: git config [<options>]
Config file location
--global use global config file
--system use system config file
--local use repository config file
--worktree use per-worktree config file
-f, --file <file> use given config file
--blob <blob-id> read config from given blob object
Action
--get get value: name [value-regex]
--get-all get all values: key [value-regex]
--get-regexp get values for regexp: name-regex [value-regex]
--get-urlmatch get value specific for the URL: section[.var] URL
--replace-all replace all matching variables: name value [value_regex]
--add add a new variable: name value
--unset remove a variable: name [value-regex]
--unset-all remove all matches: name [value-regex]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
-e, --edit open an editor
--get-color find the color configured: slot [default]
--get-colorbool find the color setting: slot [stdout-is-tty]
Type
-t, --type <> value is given this type
--bool value is "true" or "false"
--int value is decimal number
--bool-or-int value is --bool or --int
--bool-or-str value is --bool or string
--path value is a path (file or directory name)
--expiry-date value is an expiry date
Other
-z, --null terminate values with NUL byte
--name-only show variable names only
--includes respect include directives on lookup
--show-origin show origin of config (file, standard input, blob, command line)
--show-scope show scope of config (worktree, local, global, system, command)
--default <value> with --get, use default value when missing entry
在Action部分我們能看到支持的操作命令,get/replace/add/unset
對應(yīng)著查看修改增加刪除功能,不需要在搜索引擎中查找git命令了
- 查看本機(jī)的安裝文檔
git log --help
在命令行中輸入后會在瀏覽器中自動打開對應(yīng)的本地文檔
十四. git 顏色及字體
- git可用顏色值
normal
black
red
green
yellow
blue
magenta
cyan
white - git字體可選
bold
dim
ul
blink
reverse
十五. git 更改當(dāng)前用戶名及密碼
git config --system --unset credential.helper
清除以后再進(jìn)行相關(guān)的推送git操作時(shí)會要求重新輸入新的賬號密碼
如果是Windows系統(tǒng)也可以在控制面板-用戶賬戶-憑據(jù)管理器中刪除對應(yīng)倉庫的賬號和密碼即可輸入新的賬號和Person Access Token即可
十六. .gitignore文件忽略文件/文件夾及其失效解決方法
如果已寫了忽略某個(gè)文件夾并提交了, 例如: 文件夾Deploy
Deploy/**/*
再添加對某個(gè)文件的忽略,如
!Deploy/.env
此時(shí)是可以起到忽略.env文件效果的, 但是如果深一層,如 Deploy/redis/config/redis.conf
添加到.gitignore中
!Deploy/redis/config/redis.conf
此時(shí)redis.conf文件的取消忽略就失效了, 猜測是.gitignore的機(jī)制導(dǎo)致的,
解決:
- 刪除單個(gè)文件git緩存
git rm --cached Deploy/redis/config/redis.conf
或者整個(gè)目錄
git rm --cached -r Deploy/redis
如果文件很多, 可以直接
git rm --cached -r .
如果提示某個(gè)文件無法忽略, 可以添加-f
參數(shù)強(qiáng)制忽略
git rm -f --cached Deploy/redis/config/redis.conf
- 將所有文件再添加回來
git add .
git commit -m "update .gitignore"
-
[重點(diǎn)]
把被忽略的某個(gè)文件強(qiáng)制添加回去
git add -f filename
十七将鸵、git提交規(guī)范
feat:提交新功能
fix:修復(fù)了bug
docs:只修改了文檔
style:調(diào)整代碼格式勉盅,未修改代碼邏輯(比如修改空格、格式化咨堤、缺少分號等)
refactor:代碼重構(gòu)菇篡,既沒修復(fù)bug也沒有添加新功能
perf:性能優(yōu)化漩符,提高性能的代碼更改
test:添加或修改代碼測試
chore:對構(gòu)建流程或輔助工具和依賴庫(如文檔生成等)的更改
十八一喘、git 打標(biāo)簽 tag
- 添加輕量級標(biāo)簽(常用)
git tag v0.2
//推送標(biāo)簽, 只推一個(gè)
git push origin v2.1
- 顯示標(biāo)簽
git tag
v1.0
v1.2
v2.0
//過濾標(biāo)簽
git tag -l 'v2.*'
v2.0
//查看標(biāo)簽的版本 信息
git show v2.0
- 創(chuàng)建標(biāo)簽
git tag -a v2.1 -m 'my version v2.1'
-a
是annotated的首字母,指定標(biāo)簽名
-m
是message的首字母, 指定標(biāo)簽說明, 如果沒有指定-m git會啟動文本編輯供你輸入標(biāo)簽說明
- 刪除標(biāo)簽
git tag -d v2.0
- 后期加標(biāo)簽
git log --pretty=oneline
//查找特定分支上的id, 打標(biāo)簽的時(shí)候帶入即可
git tag -a v2.1 166ae0c4d3
- 推送標(biāo)簽
//一次推一個(gè)標(biāo)簽
git push origin v2.1
//一次將本地所有標(biāo)簽都推送到遠(yuǎn)端
git push origin --tags
十九、分支Branch操作
//本地相關(guān)
//查看分支
git branch
//查看當(dāng)前分支的詳情
git branch -v
git branch --verbose
//新建分支
git branch (branch name)
//切換分支
git checkout (branch name)
//合并分支(合并另一個(gè)分支到當(dāng)前分支上)
git merge (branch name)
//刪除分支(當(dāng)前不在刪除的分支上嗜暴,否則會刪除失斖箍恕)
git branch -d (branch name)
git branch --delete (branch name)
//遠(yuǎn)程相關(guān)
//查看遠(yuǎn)程倉庫的分支
git branch --remote
//拉取遠(yuǎn)程分支并同時(shí)創(chuàng)建對應(yīng)的本地分支
git checkout -b (branch name) origin/(branch name)
//將本地分支與遠(yuǎn)程保持同步
git fetch
//將本地所有分支與遠(yuǎn)程保持同步
git fetch --all
//拉取所有分支代碼
git pull --all
//刪除遠(yuǎn)程分支
git push origin :(branch name)
//將本地分支初次推送到遠(yuǎn)端
git push -u origin "(branch name)"
二十、查看已提交內(nèi)容的修改信息
git show
git show commitId
git show commitId fileName
二十一闷沥、gitflow
git flow 介紹
分支名 | 作用 |
---|---|
master | 主分支萎战,用于uat發(fā)布或最終發(fā)布,絕不可直接push |
develop | 主開發(fā)分支舆逃,基于master分支克隆蚂维,只能從其它分支合并 |
feature | 功能開發(fā)分支,基于develop分支克隆路狮,用于新功能新需求的開發(fā) |
release | 測試(sit環(huán)境)分支虫啥,提交給測試人員進(jìn)行功能測試及在本分支進(jìn)行BUG修復(fù) |
hotfix | 補(bǔ)丁分支,基于master分支克隆奄妨,用于uat或正式環(huán)境的版本進(jìn)行BUG修復(fù) |
- 初始化gitflow
git flow init -d
- 新功能分支
// git checkout -b feature/<feature-name> develop
git flow feature start <feature-name>
- 需要將本地代碼提交到遠(yuǎn)程倉庫:
// git push origin feature/<feature-name>
// git push --set-upstream origin feature/<feature-name>
// git push origin
git flow feature publish <feature-name>
- 當(dāng)功能開發(fā)完畢后就將進(jìn)入測試階段:
// git checkout develop
// git merge feature/<feature-name>
// git branch -d feature/<feature-name>
git flow feature finish <feature-name>
- release 本地提交
git flow release start <release-name|1.0.0>
- release 需要將本地代碼提交到遠(yuǎn)程倉庫
git flow release publish < release-name|1.0.0 >
- release 待測試通過后需要發(fā)布UAT版
// git fetch涂籽,拉取最新的代碼
// 將分支合并到master分支
// 生成tag
// 將分支合并到develop分支
// 刪除release/<release-name>分支
// 切換回develop分支
git flow release finish < release-name|1.0.0 >
- 推送到遠(yuǎn)程倉庫:
git push origin –all
git push origin --tag
- hotfix 場景
git flow hotfix start <hotfix-name|1.0.0.b1>
git flow hotfix finish <hotfix-name|1.0.0.b1>
git flow 基本使用至此完成
git developer分支合并到master分支
- 1.查看本地和遠(yuǎn)程分支
$ git branch -a
* developer
master
remotes/origin/developer
remotes/origin/master
- 2.切換到本的developer分支
$ git checkout -b developer origin/developer
如果developer分支已經(jīng)存在,執(zhí)行下面這步
$ git checkout developer
如查當(dāng)前使用的就是developer分支砸抛,則這步不用執(zhí)行评雌。
- 3.把遠(yuǎn)程的developer分支拉取到本地,保證是最新的developer分支
$ git pull
- 4.切換到master分支
$ git checkout master
- 5.確保存master分支也是最新的
$ git pull
- 6.執(zhí)行合并的關(guān)鍵代碼直焙,此時(shí)執(zhí)行結(jié)果時(shí)將本地的developer合并到本地master分支
$ git merge developer
- 7.將合并的本地master分支推送到遠(yuǎn)程master
$ git push origin master
將代碼推送到兩個(gè)遠(yuǎn)程倉庫
先配置
$ git remote add origin https://github.com/wwmin/***.git
$ git remote add github https://github.com/wwmin/***.git
$ git remote add gitee https://gitee.com/wwmin/***.git
后推送
$ git push github master
$ git push gitee master