roach57
Python,Linux,运维自动化,Shell

导航

 

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 结构图

img

  • 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 "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] # 新建且切换到指定分支,依据 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> # 检查本地是否存在指定分支

img

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.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 git@gitlab.cardinfo.com.cn: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 git@gitlab.cardinfo.com.cn:root/kubo_qa.git
git push -u origin --all
git push -u origin --tags

将本地分支和远程分支建立连接(本地仅创建了目录)

cd existing_folder
git init
git remote add origin git@gitlab.cardinfo.com.cn: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 发布

posted on 2021-02-07 15:24  roach57  阅读(147)  评论(0编辑  收藏  举报