git 使用手冊
設定只要輸入一次密碼
git config --global credential.helper cache
git config --global credential.helper store
git 配置
git 組態檔的權重 倉庫 > 全域 > 系統.
pro/.git/config # 倉庫配置
核心配置
[core]
excludesfile = /Users/roach/.gitignore_global # 包括一個獨立的組態檔
配置方式
洗掉一個 section
括號起來的就是 section
格式: git config [--local|--global|--system] --remove-section section
示例: git config --local --remove-section dog
查看 value 型別
一共可以查看四種型別:--bool, --int, --bool-or-int, --path
格式: git config [--local|--global|--system] [--bool|--int|--bool-or-int|--path] section.key
示例: git config --local --bool dog.name
操作特殊的 section
[remote "origin"]
url = https://xxxx.git
示例: git config --local remote.origin.url value
重命名 section
引數:--rename-section
格式:git config [--local|--global|--system] --rename-section section1 section2
示例: 比如我們把名為dog的section改為dog1, git config --local rename-section dog dog1,這樣就好咯
替換,獲取和洗掉多個屬性
引數: --replace-all, --get-all, -unset-all
示例: git config --local --replace-all dog.a 333
運用正則
引數: --replace-all, --get-regexp
示例:
例如我們來獲取配置中的core下面的所有key值的value
使用命令:get config --local --get-regexp core 即可
常用配置
別名: [alias]
配色: [color]
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "status"]
added = yellow
changed = green
untracked = cyan
[color "diff"]
meta = yellow
frag = magenta bold
commit = yellow bold
old = red bold
new = green bold
whitespace = red reverse
[color "diff-highlight"]
oldNormal = red bold
oldHighlight = red bold 52
newNormal = green bold
newHighlight = green bold 22
核心: [core]
[core]
editor = vim
excludesfile = ~/.gitignore
pager = diff-so-fancy | less --tabs=4 -RFX
autocrlf = input
憑證: [credential]
[credential]
helper = cache --timeout=28800
echo "http://$username:$password@$remotehost" >> ~/.git-credentials
git config --global credential.helper store
推送: [push]
[push]
default = current
git 結構圖

- Workspace: 作業區
- index/stage: 暫存區
- repository: 產庫區(本地倉庫)
- remote: 遠程倉庫
git 幫助查看
git [xxx] -h
git 常用命令
git init
新建代碼庫
git init # 在當前目錄新建一個 git 代碼庫
git init <project-name> # 新建一個目錄, 將其初始化為 git 代碼庫.
git clone # 下載一個專案和它的整個代碼歷史
用法:git init [-q | --quiet] [--bare] [--template=<模板目錄>] [--shared[=<權限>]] [<目錄>]
--template <模板目錄>
模板目錄將被使用
--bare 創建一個純倉庫
--shared[=<權限>] 指定 git 倉庫是多個用戶之間共享的
-q, --quiet 靜默模式
--separate-git-dir <git目錄>
git目錄和作業區分離
git clone
用法:git clone [<選項>] [--] <倉庫> [<路徑>]
-v, --verbose 更加詳細
-q, --quiet 更加安靜
--progress 強制顯示進度報告
-n, --no-checkout 不創建一個檢出
--bare 創建一個純倉庫
--mirror 創建一個鏡像倉庫(也是純倉庫)
-l, --local 從本地倉庫克隆
--no-hardlinks 不使用本地硬鏈接,始終復制
-s, --shared 設定為共享倉庫
--recurse-submodules[=<路徑規格>]
在克隆時初始化子模組
-j, --jobs <n> 并發克隆的子模組的數量
--template <模板目錄>
模板目錄將被使用
--reference <倉庫> 參考倉庫
--reference-if-able <倉庫>
參考倉庫
--dissociate 僅在克隆時參考 --reference 指向的本地倉庫
-o, --origin <名稱> 使用 <名稱> 而不是 'origin' 去跟蹤上游
-b, --branch <分支> 檢出 <分支> 而不是遠程 HEAD
-u, --upload-pack <路徑>
遠程 git-upload-pack 路徑
--depth <深度> 創建一個指定深度的淺克隆
--shallow-since <時間>
從一個特定時間創建一個淺克隆
--shallow-exclude <版本>
深化淺克隆的歷史,除了特定版本
--single-branch 只克隆一個分支、HEAD 或 --branch
--no-tags 不要克隆任何標簽,并且后續獲取操作也不下載它們
--shallow-submodules 子模組將以淺下載模式克隆
--separate-git-dir <git目錄>
git目錄和作業區分離
-c, --config <key=value>
在新倉庫中設定配置資訊
-4, --ipv4 只使用 IPv4 地址
-6, --ipv6 只使用 IPv6 地址
--filter <引數> 物件過濾
git config
配置 git
git config -l # 顯示所有配置
git config --local -l # 顯示當前 git 的配置
git config --local -e # 打開組態檔
xxx/.git/config
git config --global -l # 查看用戶配置
/home/user/.gitconfig
git config --system -l # 查看系統配置
/usr/lcal/etc/gitconfig
git config --global # 全域配置 git
git config user.email "[email protected]" # 配置當前專案的組態檔
git config user.name "xxx" # 配置當前專案的用戶名
用法:git config [<選項>]
組態檔位置
--global 使用全域組態檔
--system 使用系統級組態檔
--local 使用倉庫級組態檔
-f, --file <檔案> 使用指定的組態檔
--blob <資料物件 ID> 從給定的資料物件讀取配置
操作
--get 獲取值:name [value-regex]
--get-all 獲得所有的值:key [value-regex]
--get-regexp 根據正則運算式獲得值:name-regex [value-regex]
--get-urlmatch 獲得 URL 取值:section[.var] URL
--replace-all 替換所有匹配的變數:name value [value_regex]
--add 添加一個新的變數:name value
--unset 洗掉一個變數:name [value-regex]
--unset-all 洗掉所有匹配項:name [value-regex]
--rename-section 重命名小節:old-name new-name
--remove-section 洗掉一個小節:name
-l, --list 列出所有
-e, --edit 打開一個編輯器
--get-color 獲得配置的顏色:配置 [默認]
--get-colorbool 獲得顏色設定:配置 [stdout-is-tty]
型別
-t, --type <> 取值為該型別
--bool 值是 "true" 或 "false"
--int 值是十進制數
--bool-or-int 值是 --bool or --int
--path 值是一個路徑(檔案或目錄名)
--expiry-date 值是一個到期日期
其它
-z, --null 終止值是 NUL 位元組
--name-only 只顯示變數名
--includes 查詢時參照 include 指令遞回查找
--show-origin 顯示配置的來源(檔案、標準輸入、資料物件,或命令列)
--default <取值> 使用 --get 引數,當缺少設定時使用默認值
git add
增加/洗掉檔案
git add [file1] [file2] # 添加指定檔案到暫存區
git add [dir] # 添加指定目錄到暫存區, 包括子目錄
git add . # 添加當前目錄的所有檔案到暫存區.
用法:git add [<選項>] [--] <路徑規格>...
-n, --dry-run 演習
-v, --verbose 冗長輸出
-i, --interactive 互動式揀選
-p, --patch 互動式挑選資料塊
-e, --edit 編輯當前差異并應用
-f, --force 允許添加忽略的檔案
-u, --update 更新已跟蹤的檔案
--renormalize 對已跟蹤檔案(暗含 -u)重新歸一換行符
-N, --intent-to-add 只記錄,該路徑稍后再添加
-A, --all 添加所有改變的已跟蹤檔案和未跟蹤檔案
--ignore-removal 忽略作業區中移除的路徑(和 --no-all 相同)
--refresh 不添加,只重繪索引
--ignore-errors 跳過因出錯不能添加的檔案
--ignore-missing 檢查在演習模式下檔案(即使不存在)是否被忽略
--chmod (+|-)x 覆寫串列里檔案的可執行位
git stash
臨時保存開發進度到堆疊中, 會把暫存區和作業區的改動保存起來.
git stash list # 查看已保存的進度串列
git stash save '注釋' # 保存當前作業進度
git stash pop [--index] [stash_id] 恢復后進度也隨之洗掉
git stash pop # 恢復到最近的一個進度到作業區, 恢復的內容包括作業區和暫存區的改動
git stash pop --index # 恢復最近的一個進度作業區和暫存區
git stash pop stash@{1} # 恢復指定的進度到作業區
git stash apply [--index] [stash_id] # 恢復后進度不洗掉
git stash drop [stash_id] # 洗掉指定進度
git stash clear # 洗掉所有進度
git stash show [stash_id] [-p]# 查看堆疊中保存的 stash 和當前目錄的差異. -p 顯示詳情
git stash show stash@{1}
git stash branch # 從最新的 stash 創建分支.
git stash apply stash@{1} // 恢復原來的作業環境
git commit
git commit -m "message" # 提交代碼
git commit --amend -m "message" # 覆寫上一次提交的 commit, 防止 commit 過多
git branch
只對分支做操作, 不會切換分支.
git branch # 列出所有本地分支
git branch -r # 列出所有遠程分支
git branch -a # 列出所有遠程分支和本地分支
git branch [new-branch] # 新建一個分支, 但是仍然停留在當前分支
git branch [new-branch] [commit] # 依據指定 commit 創建新分支
git branch --track [new-branch] [remote-branch] # 新建一個分支, 與指定的遠程分支建立追蹤關系
git branch --set-upstream [branch] [remote-branch] # 建立追蹤關系,在指定分支與遠程指定的遠程分支之間
git branch -d # 洗掉分支
git push origin --delete [branch-name] # 洗掉遠程分支
git push -dr [origin/branch-name] # 上一條的縮寫
git checkout
git checkout -b # 新建分支且切換到該分支
git checkout # 切換到指定分支
git checkout - # 切換到上一個分支
git checkout -b [branch] [tag] # 新建且切換到指定分支,依據 tag
git checkout -b [branch] [commit] # 新建且切換到指定分支,依據 commit
git checkout --track orgin/branch_name, 本地沒有該分支, 這時會新建一個分支叫 branch_name, 并自動跟蹤遠程的同名分支.(在本地創建)
git checkout [commit] [file] # 恢復某個 commit 的指定檔案到暫存區和作業區
git checkout . # 恢復暫存區的所有檔案到作業區(取消 add 操作)
git merge
將指定分支合并到當前分支
git merge [branch] # 合并指定分支到當前分支.
git log
git log --online -3 # 查看遠程倉庫最近三次提交
git log -S [keyword] # 搜索提交歷史, 根據關鍵詞
git log --follow [file] # 查看某個檔案的歷史版本
git log -5 --pretty --online # 顯示過去 5 次提交
git blame [file] # 顯示檔案被哪些人改過
git cherry-pick
調減合并
git cherry-pick [commit-id] 將指定 commit [可跨分支]合并到當前分支, 并提交
git cherry-pick -n # 合并后不自動提交
git cherry-pick -e # 合并后自動提交, 并自定義提交資訊.
git tag
git tag # 列出所有 tag
git tag [tag] # 新建一個 tag
git tag -d [tag] # 洗掉本地 tag
git show [tag] # 查看 tag 資訊
git tag -a v1.4 -m 'version 1.4'
git push
git push --set-upstream orgin branch_name # 在遠程創建一個與本地 branch_name 分支同名的分支并跟蹤 (在遠程創建)
git push origin :refs/tags/[tagName] # 洗掉遠程 tag
git push [remote] [tag] # 提交指定 tag
git push [remote] --tags # 提交所有 tags
git push [remote] [branch] # 上傳本地指定分支到遠程倉庫
git push [remote] --force # 強行推送到遠程分支.
git push [remote] --all # 推送所有分支到遠程倉庫
git push origin xxx # 將本地分支推送到遠程分支 xxx(遠程分支不存在), 并系結關聯
git push origin --delete [branch-name] # 洗掉遠程分支
git push -dr [origin/branch-name] # 上一條的縮寫
git pull
git pull [remote] [branch] # 取回遠程倉庫的變化,并與本地分支合并.
git pull [remote] --all # 拉取所有遠程分支
git status
git status # 顯示有變更的檔案
git
git diff
git diff # 顯示暫存區和作業區的的差異
git diff HEAD # 顯示作業區和當前分支最新 commit 之間的差異
git diff [first-branch]...[seconde-branch] # 顯示兩次提交之間的差異
git diff --shortstat "@{0 day ago}" # 顯示今天你寫了多少行代碼.
git show
git show # 顯示某次提交的元資料和內容變化
git show --name-only [commit] # 顯示某次提交發生變化的檔案
git show [commit]:[filename] # 顯示某次提交時,某個檔案的內容
git reflog
git reflog # 顯示當前分支的最近幾次提, 哪怕 reset 后也能看到
git fetch
git fetch [remote] # 下載遠程倉庫的所有變動(當同事將代碼提交到遠程的時候,而你本地卻看不到更新時使用)
git remote
git remote -v # 顯示所有遠程倉庫
git remote show [remote] # 顯示某個遠程倉庫的資訊
git remote add [shortname] [url] # 增加一個新的遠程倉庫
git reset
不會保留歷史的 commit 提交
git reset [file] # 重置暫存區的指定檔案,與上一次 commit 保持一致,但作業區不變
git reset --hard [commit]#重置暫存區與作業區, 與上一次 commit 保持一致(使用比較頻繁的操作), 重新指定到指定 commit 的版本, 且"中間版本"都不再使用的情況下使用.
git push -f # 本地比遠程舊, 需要強推
git revert
會保留歷史的 commit 提交
git revert [commit_id] # 新建一個 commit, 用來撤銷指定 commit, 后者的所有變化都會被前者抵消,并且應用到當前分支(如果我們想撤銷之前的某一版本,但是又想保留該目標版本后面的版本(中間版本),記錄下這整個版本變動流程,就可以用這種方法,)
git revert -n [commit_id] # -n 不自動提交
git commit -m "xx" # 手動提交新的內容
git push
git archive
生成一個可供發布的壓縮包
打包建議在代碼庫的根目錄下進行,不然會碰到各種問題,
在下一級目錄執行打包,默認只打包該目錄下的內容.
git archive -l # 顯示支持的壓縮包
git archive --format tar.gz --output "./output.tar.gz" master # 根據指定分支生成一個壓縮包
git archive --output "./output.tar.gz" # 根據生成的檔案名進行指定格式的壓縮
git archive --format tar.gz --output "./output.tar.gz" 5ca16ac0d603603 # 根據指定 commit 打包
git rm
git rm -r --cached . # 清除快取
git rev-parse
git rev-parse HEAD # 顯示最新的 git commit id
git rev-parse --verify <branch_name> # 檢查本地是否存在指定分支

git 使用場景
本地 git 用戶配置
- 配置SSH-KEY:
http://10.10.116.61/help/ssh/README
- 全域配置:
git config --global user.name "吳強"
git config --global user.email "[email protected]"
- 區域配置:
git config user.name "吳強"
git config user.email "[email protected]"
創建一個新的本地倉庫
git clone [email protected]:root/kubo_qa.git
cd kubo_qa
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
替換遠程分支
cd existing_folder
git init
git remote add origin [email protected]:root/kubo_qa.git
git add .
git commit
git push -u origin master
將本地分支和遠程分支建立連接(本地已經存在代碼)
cd existing_repo
git remote rename origin old-origin
git remote add origin [email protected]:root/kubo_qa.git
git push -u origin --all
git push -u origin --tags
將本地分支和遠程分支建立連接(本地僅創建了目錄)
cd existing_folder
git init
git remote add origin [email protected]:root/kubo_qa.git
git add .
git commit -m "Initial commit"
git push -u origin master (如果報錯: src refspec master does not match any 執行[
git push --set-upstream origin master
])
創建分支
git branch wuqiang_kubo
創建分支并切換分支
git checkout -b [branch_name]
git checkout -b wuqiang_kubo
git checkout -b kcmdb_dev_future
切換分支
git checkout [branch_name]
git checkout wuqiang_kubo
提交變更, 提交回滾
1. 提交變更
git add *
git commit -m "information"
git push -n origin master
git push origin //推送到已關聯當前本地分支的遠程分支
git push origin master //推送到已經關聯當前本地分支的遠程分支, 如果該遠程分支不存在則會自動創建該遠程分支.
1.1. 推送本地分支local_branch到遠程分支 remote_branch并建立關聯關系
a.遠程已有remote_branch分支并且已經關聯本地分支local_branch且本地已經切換到local_branch
git push
b.遠程已有remote_branch分支但未關聯本地分支local_branch且本地已經切換到local_branch
git push -u origin/remote_branch
c.遠程沒有remote_branch分支 and 本地已經切換到local_branch
git push origin local_branch:remote_branch
2. 提交回滾
git log // 查看歷史提交
git revert sfsdfwefsdfwe // 回滾到指定版本
臨時修復 Bug
保存臨時作業區[作業到一半,突然需要緊急修復某個 bug. 需要切出到某個分支修復 bug, 修復完后切會保存的作業區中繼續之前的開發作業.]
git stash save "work in progress for foo feature" // 保存當前作業區到堆疊中
git branch another_bug // 創建bug修復分支
git checkout another_bug // 切換到bug修復分支, 進行bug修復
-- 修復完成 --
git add .
git commit -m "xxxx"
git push -u origin/master // 將修復后的分支提交到上線分支上,并建立關聯關系
-- bug修復版上線完 --
git stash list // 羅列已保存在堆疊中的所有作業區
git checkout xxx_dev // 切換回開發分支
git stash apply stash@{1} // 恢復原來的作業環境
洗掉分支
git branch -d # 洗掉本地分支
git branch -d branchname # 洗掉本地分支(當用戶就在這個分支上時,該操作不執行)
git branch -D branchname # 強制洗掉本地分支
git branch -r -D origin/Branchname # 洗掉暫存區分支
git push origin --delete [branch-name] # 洗掉遠程分支
git push -dr [origin/branch-name] # 上一條的縮寫
分支重命名
git branch -m oldbranch newbranch // 重命令
git branch -M oldbranch newbranch // 強制重命名
將檔案從 tracked 中移除
添加忽略檔案
在專案目錄下放這個檔案 cat .gitignore
?```
.gitignore
.idea/
#python
*.py[cod]
*.so
*.egg
*.egg-info
dist
build
?```
git rm -r --cached .idea/
git add xxx // 將目錄添加到tracked中, 這將會使得.gitignore檔案中的相關配置失效.
git commit -m "洗掉.idea目錄"
git push origin kcmdb_dev
git rm .idea
打 tag
git tag -a v1.4 -m 'version 1.4'
克隆指定的分支或者指定的 tag
git clone <remote-addr:repo.git> -b <branch-or-tag-or-commit>
例子: git clone http://gitlab.cardinfo.com.cn/cmdb/kubo_forward.git -b kforward-0.1.0
恢復誤洗掉的檔案
查看作業區狀態
git status
要查看洗掉的檔案: git ls-files --deleted
使用命令checkout來恢復:git checkout -- file_name
如果要恢復多個被洗掉的檔案,可以使用批處理命令:
git ls-files -d | xargs git checkout --
如果要恢復被修改的檔案,命令:git ls-files -m | xargs git checkout --
查看某個檔案的詳細修改記錄
git log -p files
撤銷檔案修改
如果想放棄本地的檔案修改,可以使用
git reset --hard FETCH_HEAD // 回到某次提交, 但是此次之前的修改都會被退回到暫存區[不推薦].
git revert //生成一個新的提交來撤銷某次提交,能留下歷史記錄.此次提交之前的commit都會被保留
git revert HEAD^ //回退到上一個版本.
git revert HEAD~n n表示回退的層數
git reset HEAD~n
git reset -h | git reset --help //查看幫助
檢查本地是否存在指定分支
git rev-parse --verify <branch_name>
參考鏈接
gitignore.io
本文由博客群發一文多發等運營工具平臺 OpenWrite 發布
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/257697.html
標籤:其他
上一篇:MFC多檔案繪圖管理
下一篇:git 使用手冊
