git 使用手册
设置只有输出一次明码
git config --global credential.helper cachegit 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 reverselocal = yellowremote = green[color "status"]added = yellowchanged = greenuntracked = cyan[color "diff"]meta = yellowfrag = magenta boldcommit = yellow boldold = red boldnew = green boldwhitespace = red reverse[color "diff-highlight"]oldNormal = red boldoldHighlight = red bold 52newNormal = green boldnewHighlight = 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-credentialsgit 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/configgit config --global -l # 查看用户配置 /home/user/.gitconfiggit config --system -l # 查看系统配置 /usr/lcal/etc/gitconfiggit config --global # 全局配置 gitgit config user.email "xxx@xxx.com" # 配置以后我的项目的配置文件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] # 新建且切换到指定分支,根据 taggit checkout -b [branch] [commit] # 新建且切换到指定分支,根据 commitgit 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 # 列出所有 taggit tag [tag] # 新建一个 taggit tag -d [tag] # 删除本地 taggit 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] # 删除近程 taggit push [remote] [tag] # 提交指定 taggit push [remote] --tags # 提交所有 tagsgit 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 idgit 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 "qiang.wu@cardinfo.com.cn"- 部分配置:git config user.name "吴强"git config user.email "qiang.wu@cardinfo.com.cn"
创立一个新的本地仓库
git clone git@gitlab.cardinfo.com.cn:root/kubo_qa.gitcd kubo_qatouch README.mdgit add README.mdgit commit -m "add README"git push -u origin master
替换近程分支
cd existing_foldergit initgit remote add origin git@gitlab.cardinfo.com.cn:root/kubo_qa.gitgit add .git commitgit push -u origin master
将本地分支和近程分支建设连贯(本地曾经存在代码)
cd existing_repogit remote rename origin old-origingit remote add origin git@gitlab.cardinfo.com.cn:root/kubo_qa.gitgit push -u origin --allgit push -u origin --tags
将本地分支和近程分支建设连贯(本地仅创立了目录)
cd existing_foldergit initgit remote add origin git@gitlab.cardinfo.com.cn:root/kubo_qa.gitgit 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_kubogit 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_branch2. 提交回滚 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-infodistbuild```git rm -r --cached .idea/git add xxx // 将目录增加到tracked中, 这将会使得.gitignore文件中的相干配置生效.git commit -m "删除.idea目录"git push origin kcmdb_devgit 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~ngit reset -h | git reset --help //查看帮忙
查看本地是否存在指定分支
git rev-parse --verify <branch_name>
参考链接
gitignore.io
本文由博客群发一文多发等经营工具平台 OpenWrite 公布