解决windows环境下的CRLF与unix环境下的LF问题,windows提交时CRLF=>LF,签出时LF=>CRLF,unix环境保留

git config --global core.autocrlf input

修改文件格式(修复显示'^M')

: set ff
# 修改文件格式(换行符)
: set ff=dos/unix

忽略查看差异时显示'^M'

git diff -b
# or
git diff --ignore-cr-at-eol
# or
git diff --ignore-space-change
# config
git config --global core.whitespace cr-at-eol
# unset config
git config --global --unset core.whitespace

查看冲突的文件

git ls-files -u | cut -f 2 | sort -u
# or
git diff --name-only --diff-filter=U

查看冲突的内容

git diff conflict-file

解冲突

# 添加修改点
git add
git commit
git push
# 提示本地仓晚于远端仓,需要pull
# --rebase 将本次提交移至pull之后的最新节点
git pull --rebase
# 提示冲突,解冲突后添加
git add
git rebase --continue
git push

批量修改提交历史中的用户和邮箱

git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "history@email.com" ];
then
GIT_AUTHOR_NAME="new_author";
GIT_AUTHOR_EMAIL="new_email@email.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD

修改刚提交的提交信息,如用户和邮箱

git commit --amend --author "new_author <new_email@email.com>" --no-edit

修改指定提交的提交信息,如用户和邮箱

git rebase -i commit-id^
git commit --amend --author "new_author <new_email@email.com>" --no-edit
git rebase --continue

追加tag到指定commit

git tag -a <tag> -m <message> <commit-id>

恢复误把新提交追加到上次提交

git reflog
# 找到上次提交节点commit-id
git reset commit-id

追加新提交到指定提交

git stash push -m "贮藏已修改的内容"
git rebase -i commit-id^
# 在弹出的提交信息中找到要追加到的那笔commit,将前面的pick改为edit,保存退出
# 在此状态下可对文件任意修改,诸如撤回某个文件 git reset file
git stash pop
git add
git commit --amend
git push
git rebase --continue

基于Gerrit 在代码仓追加新提交到指定提交(不限用户)(Gerrit使用Change-Id来标识提交,要合并为同一提交保持Change-Id不变即可)

# 获取到Gerrit上已存在的提交的 Change-Id
git add
git commit
# 在弹出的提交信息中修改 Change-Id 为 Gerrit 上存在的提交的 Change-Id
git push

压缩多笔提交为1笔

# 在弹出的提交信息中除要保留的那笔commit(使用这笔提交的提交信息),将其他提交前面的pick改为fixup,注意要将使用提交信息的提交移至最顶上
git rebase -i HEAD~n
git push # 若只是将提交中的某几个合并为1个提交,将要操作的这几笔提交均覆盖到编辑范围内,在其中要操作的这几笔提交前面的pick改为fixup,但要注意保留提交信息的提交在最顶上
git rebase -i <commit-id> # 或回退到指定提交的上一个,然后在工作区重新commit,此法也方便stash
git reset <commit-id^>
# 注意这个n,git log输出提交信息,自顶向下数,要回退到那笔提交是第几个,n就是几
git reset HEAD~n

push指定commit(应对多个未push的commit)

# 原则就是将要push的commit移动至工作仓最早的即第一个提交位置
git rebase -i <commit-id>^
# 在弹出的文件修改中使用命令 ":m 0" 将该提交移至第一行
git push origin <commit-id>:remote repository
# 将包括commit-id之前的所有未推送的提交push,故push哪一笔或哪几笔就在此做文章

删除指定提交

git rebase -i commit-id^
# 在弹出的提交信息中找到要删除的那笔commit,将前面的pick改为drop或是直接删除该行
# or
git reset --hard commit-id^
git cherry-pick commit-id..commit-id-latest
# commit-id是要删除的提交,commit-id-latest是未处理之前的最新一笔提交 git push -f origin remote-branch

干净工作仓

# 清除已跟踪文件, '.' 表示执行指令的当前路径,即递归当前目录回退到上次提交的状态
git checkout .
# 清除未跟踪文件
git clean -df
# 预览将要删除的文件
git clean -ndf

取消已跟踪的文件或文件夹

git rm --cached <file>
git rm -r --cached <path>

查看工作区某类型文件的修改列表

git diff --stat *.c *.cpp *.h

查看两次提交文件修改列表

# 注意 HEAD^ HEAD 顺序
git diff --name-filter HEAD^ HEAD
# 针对新增、删除、修改单独列出文件
git diff --diff-filter=A --name-filter HEAD^ HEAD

查看具体差异

# 查看暂存区与工作区的差异
git diff
# 查看暂存区与上次提交的差异
git diff --cached
# or
git diff --staged
# 查看自上次提交以来的修改(包括暂存区和工作区)
git diff HEAD # 查看两次提交差异
git diff commit-id1 commit-id2
# 查看两个分支差异
git diff branch1 branch2
# 具体文件差异
git diff branch1 branch2 <file>
# 文件级别差异
git diff --stat branch1 branch2

查看提交级别差异

# 分支1多于分支2的提交
git log branch1 ^branch2
# 分支2多于分支1的提交
git log branch1..branch2
# 列出分支差异提交,用'<''>'标识
git log -lefg-right branch1...branch2
# 提交2多于提交1的提交(包含提交2,即提交2是较新提交)
git log commit1..commit2
# 标签2多于标签1的提交
git log tag1..tag2

查看自上次Tag后提交次数

# 输出格式:tag-次数-g+hash
git describe

查看文件行是谁修改的

git blame <filename> -L start_line,end_line

查看文件行的修改历史

git log -L start_line,end_line:<filename>

拉取指定分支的代码

git clone -b target-branch repository.git

撤销--amend追加的commit

# 找到 commit(amend)对应的 HEAD@{n}
git reflog
git reset HEDA@{n+1}

回退

# 回退文件至暂存区
git reset --soft commit-id
# 回退文件至工作区
git reset commit-id
# 回退文件至上次提交状态
git reset --hard commit-id

带时间显示的操作历史

git reflog --date=iso

版本回退

# 抹掉提交记录,常用于回退节点后的提交均丢弃
git reset --hard commit-id
git push -f git.repository
# 保留提交记录,常用于回退节点后的某些提交需要保留,仅撤销特定提交
git revert -n commit-id
git commit
git push

查看两个分支的父节点

git merge-base branch-A branch-B
git log --oneline --graph --decorate branch-A branch-B

挑拣文件的部分修改提交(交互式暂存

git add -p
# Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
# y - stage this hunk # 暂存本块
# n - do not stage this hunk # 放弃本块
# a - stage this and all the remaining hunks in the file # 暂存本块及本文件剩余的块
# d - do not stage this hunk nor any of the remaining hunks in the file # 放弃本块及本文件剩余的块
# g - select a hunk to go to # 跳转到要选择的块
# / - search for a hunk matching the given regex # 正则匹配块
# j - leave this hunk undecided, see next undecided hunk # 暂不确认本块,查看下一个不确认的块
# J - leave this hunk undecided, see next hunk # 暂不确认本块,查看下一个确认的块
# k - leave this hunk undecided, see previous undecided hunk # 暂不确认本块,查看上一个不确认的块
# K - leave this hunk undecided, see previous hunk # 暂不确认本块,查看下一个确认的块
# s - split the current hunk into smaller hunks # 分割本块为更小块
# e - manually edit the current hunk # 手动编辑当前块
# ? - print help

贮藏文件但保留暂存区修改(贮藏与清理

git stash --keep-index

贮藏指定文件

git stash push -m "description" <file>

从贮藏恢复指定文件

git checkout stash@{n} -- <file>

查询指定分支的操作历史

git reflog show <branch>

git rev-parse

# 查询当前所在分支
git branch --show-current
# or
git rev-parse --abbrev-ref --short HEAD
# 查询当前hash值
git rev-parse --short HEAD

git cherry-pick

# 同步最近一笔提交
git cherry-pick feature # 将feature分支的最近一笔提交应用到当前分支
# 同步指定一笔提交
git cherry-pick Hash-A
# 同步多笔指定提交
git cherry-pick Hash-A Hash-B
# 同步多笔连续提交
git cherry-pick Hash-A..Hash-B # 不包含Hash-A这笔提交, A提交早于B
git cherry-pick Hash-A^..Hash-B # 包含Hash-A这笔提交

远端仓多分支情况,本地创建分支如何绑定远端分支

git checkout -b local origin/remote

查看本地分支关联的远端分支

git branch -vv
# 处在 detached 状态
git branch -a |grep "\->"

本地分支关联远端分支

git branch --set-upstream-to=origin/<branch> local-branch

新建分支带走 detached 状态下的提交

git checkout -b temp-branch <commit-id>
git checkout -b local origin/remote
git merge temp-branch
git branch -D temp-branch
# or
git checkout -b new-branch <commit-id>
git branch --set-upstream-to=origin/<branch> new-branch

在指定节点创建分支

git checkout -b new-branch <commit-id>

恢复 detached 状态

git checkout <commit-id>
## 恢复到最新HEAD
# detached 的 HEAD 得看历史了
git reflog
# 查看本地所有分支的HEAD指向信息
git for-each-ref --sort=-committerdate refs/heads --format='%(committerdate:iso8601) %(refname:short) %(objectname:short) %(contents:subject)'

查看提交的文件状态,Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), changed (T), Unmerged (U), Unknown (X), Broken (B).

git log --raw
git show --stat
git show --raw # [*] 小写字母即排除
git show --stat --diff-filter=[(A|C|D|M|R|T|U|X|B)…​[*]]

查看提交信息,作者、提交者、作者提交时间、提交者提交时间

git log --pretty=fuller

指定格式查看提交历史

git log --graph --pretty="%C(yellow) Hash: %h %C(blue)Date: %ad %C(red) Message: %s " --date=human

查看指定字符串的提交历史

# 若存在预期的修改,则输出至文件查找对应的commit-id
git log -p |grep "string"
# 利用变化前后增减的字符串查找预期的提交
git log -p -S "string"

查看差异时排除指定目录

git diff ':!'$dir_exclude''
git diff --stat :!'$dir_exclude''

查看指定commit所在分支

git branch --contains <commit-id>

补丁生成与应用

git diff > code.diff/code.patch
# 检查补丁格式
git apply --stat code.diff/code.patch
# 测试patch能否应用于本分支
git apply --check code.diff/code.patch
# 检查确认则应用补丁
git apply code.diff/code.patch

本地没有分支如何同步代码

git pull origin remote-branch

repo同步单代码仓

# code_dir:代码目录,code_dir/framework/base:单仓目录,project-name:framework/base
repo sync <project-name>

repo检查修改涉及代码仓

repo forall -p -c git diff --stat *.c *.cc *.cpp *.h | tee repo.diff

repo forall 执行多条命令

# 应用某个贮藏注释说明涉及的代码修改
repo forall -p -c "git stash list |grep ${!#} |grep -o '{.*}' |sed -e 's/{//g' |sed -e 's/}//g' |xargs -I % git stash apply %"

repo生成代码仓某个节点的快照

# 将当前代码仓信息写入xml文件
repo manifest -o name.xml
# 将当前代码仓携带版本信息写入xml文件
repo manifest -o name.xml -r
# 拷贝新生成的快照xml到manifest路径.repo/mainifest
cp name.xml .repo/mainifest
# 按照快照清单初始化代码仓
repo init -m name.xml
# 更新代码
repo sync

repo查看不同节点xml之间的提交差异

# 在仓根目录,具体用法可参见 repo help diffmanifests
repo diffmanifests [manifest1.xml [manifest2.xml] [options]

【Git】用法小记的更多相关文章

  1. [转]HttpClient的超时用法小记

    HttpClient的超时用法小记 HttpClient在使用中有两个超时时间,是一直接触和使用的,由于上次工作中使用httpClient造成了系统悲剧的情况,特地对它的两个超时时间进行了小小的测试, ...

  2. Matlab norm 用法小记

    Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p)   Returns sum(abs(A).^p)^(1/p), for ...

  3. linux expect, spawn用法小记

    linux expect, spawn用法小记_IT民工_百度空间 linux expect, spawn用法小记 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://sys ...

  4. [转载]expect spawn、linux expect 用法小记

    原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...

  5. Shell常见用法小记

    shell的简单使用 最近发现shell脚本在平常工作中简直算一把瑞士军刀,很多场景下用shell脚本能实现常用的简单需求,而之前都没怎么学习过shell,就趁机把shell相关的语法和常见用法总结了 ...

  6. git用法之常用命令[克隆、提交]

    1.克隆/下载项目 1)git clone git@git.soydai.cn:liuxuewen/static-file-3.0.git 或者 2)git clone http://git.soyd ...

  7. git用法

    chapter: 8 add 添加文件内容至索引 用法:git add [选项] [--] ... -n, --dry-run 演习 -v, --verbose 冗长输出 -i, --interact ...

  8. Bitbucket - 用git 用法

    核心流程: 从远端中心repo那里Git clone 到本地,再在本地开发(add, commit), 通常会利用branch管理,如果觉得code 没问题了,就push到远端的中心repo上.这里中 ...

  9. git用法大全

    转载自实验楼,之前有更新过两篇git的文章,毕竟内容太少,而git还有很多更丰富的技能,在实验楼上有一系列全的教程,这里做一下备案.需要时查阅. Git 实战教程 目录 一.实验说明 二.git的初始 ...

  10. git用法汇总

    使用了一年多的git命令了,昨晚竟然又出现了问题.虽然解决了,不过还是被罚了... 总结下自己常用的git命令和遇到的一些坑. 1)常用的命令 1. 从git远程分支clone代码: git clon ...

随机推荐

  1. 安装华企盾DSC防泄密系统huawei Intel的电脑,加载驱动失败

    解决方法:从控制面板-[启用或关闭Windows功能]里面把[Hyper-V的功能]关闭 重启电脑再开启之后可以加密驱动则可以加载成功

  2. Go 泛型之泛型约束

    Go 泛型之泛型约束 目录 Go 泛型之泛型约束 一.引入 二.最宽松的约束:any 三.支持比较操作的内置约束:comparable 四.自定义约束 五.类型集合(type set) 六.简化版的约 ...

  3. vscode下载

    vscode下载 想下载官网可以直接去官网下载(速度超慢~) 百度网盘链接: 链接:https://pan.baidu.com/s/14DfWI5b2HA_J_kHBcu8JQw?pwd=yyds 提 ...

  4. OSG嵌入QT的简明总结2

    正文 我之前在这篇博文<OSG嵌入QT的简明总结>中论述了OSG在QT中显示的可视化问题.其中提到官方提供的osgQt项目(地址:https://github.com/opensceneg ...

  5. 技术实操丨使用ModelArts和HiLens Studio完成云端验证及部署

    前言 HiLens Studio公测也出来一阵子了,亮点很多,我前些天也申请了公测,通过后赶快尝试了一下,不得不说真的很不错啊,特别是支持云端编辑代码,调试,甚至可以直接运行程序,即使自己的HiLen ...

  6. 技术解读丨GaussDB数仓高可用容灾利器之逻辑备份

    摘要:GaussDB数仓的Roach工具,同时提供物理备份和逻辑备份两种主要形态的备份.逻辑备份针对数据库的逻辑对象进行抽取和备份,能够有效地应对单表.schema级等较细粒度的备份,较为灵活和便利. ...

  7. Scala学习系列(二)——环境安装配置

    Scala下载地址:https://www.scala-lang.org/download/ 一.安装JDK 首先,因为Scala是运行在JVM平台上的,所以安装Scala之前要安装JDK 二.二进制 ...

  8. 如何注册appuploader账号​

    如何注册appuploader账号​ 我们上一篇讲到appuploader的下载安装,要想使用此软件呢,需要注册账号才能使用,今​ 天我们来讲下如何注册appuploader账号来使用软件.​ 1.A ...

  9. 熔断、限流、降级 —— SpringCloud Alibaba Sentinel

    Sentinel 简介 Sentinel 是阿里中间件团队开源的,面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流.流量整形.熔断降级.系统负载保护.热点防护等多个维度来帮助开发者保 ...

  10. JVM学习-自动内存管理

    文章原文:https://gaoyubo.cn/blogs/6997cf1f.html 一.运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域 ...