解决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. URL安全的Base64算法

    URL安全的Base64算法 base64是取3个字节(24位)的数据强行组成4个字节(32位)的数据做为一个分组,24位分成4个字节的话,每个字节分到6位,然后前面补两位0,然后这个字节的值位索引去 ...

  2. ElasticSearch之cat nodeattrs API

    命令样例如下: curl -X GET "https://localhost:9200/_cat/nodeattrs?v=true&pretty" --cacert $ES ...

  3. 基于Docker 部署 Seafile+OnlyOffice+Wiki插件

    原文:基于 Docker 部署 SeafilePro + OnlyOffice(CentOS版) 官方文档:用 Docker 部署 Seafile 服务 CentOS 服务器 基于 Docker 部署 ...

  4. 川普真会说中文?连嘴型都同步,用VideoReTalking一键生成你的AI播报员

    你能想到这种画面吗?霉霉在节目中用普通话接受采访,特朗普在老家用中文脱口秀,蔡明老师操着一口流利的英文调侃潘长江老师.. 这听起来似乎很魔幻,可如今全部由VideoReTalking实现了 你只需要传 ...

  5. P6357 题解

    Luogu 题面 题目描述 给定一串长度为 \(n\) 的数字,数字为 \(0 \sim 9\) 之间的任意一个,下标从 \(1\) 记起. 然后进行 \(m\) 次区间查询,每次查找区间 \([l, ...

  6. K8S系列一:概念入门

    K8S系列一:概念入门 写在前面 本文组织方式: K8S的架构.作用和目的.需要首先对K8S整体有所了解. K8S是什么? 为什么是K8S? K8S怎么做? K8S的重要概念,即K8S的API对象.要 ...

  7. JavaImprove--Lesson02--Object类,Objects工具类,封装类

    一.Object类 Java中的Object类是所有类的超类,它是Java类层次结构的根类.这意味着所有的类都直接或间接地继承自Object类 equals(Object obj): 用于比较两个对象 ...

  8. LeetCode DP篇-求子序列问题(1143、300、53、72)

    1143. 最长公共子序列 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度. 一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情 ...

  9. 华为云数据治理生产线DataArts,让“数据‘慧’说话”

    摘要:数据治理生产线DataArts改变了传统"人拉肩抗"的数据处理方式,帮助提升效率:降低技术门槛,让"人人都是分析师":让"数据'慧'说话&quo ...

  10. DBA:介里有你没有用过的“CHUAN”新社区版本Redis6.0

    摘要:华为云DCS Redis 6.0社区版带来了极致性能.功能全面.可靠性强.性价比高的云服务,并且完全兼容开源Redis,客户端无需修改代码,开通后即可使用,使企业完全无需后顾之忧就能享受到业务响 ...