解决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为平面设计公司提供数据防泄漏解决方案

    华企盾DSC作为一款专业的数据防泄漏解决方案,为平面设计公司提供多方位而有效的安全保障.以下是该解决方案为平面设计公司所带来的主要优势: 图纸加密保护: 超安全的加密技术确保设计公司的图纸和敏感信息得 ...

  2. ElasticSearch之Open index API

    打开指定的索引. 命令样例如下: curl -X POST "https://localhost:9200/testindex_003/_open?pretty" --cacert ...

  3. 基于WebRTC的局域网文件传输

    基于WebRTC的局域网文件传输 WebRTC(Web Real-Time Communications)是一项实时通讯技术,允许网络应用或者站点,在不借助中间媒介的情况下,建立浏览器之间点对点P2P ...

  4. 在C#中,如何以编程的方式设置 Excel 单元格样式

    前言 在C#开发中,处理Excel文件是一项常见的任务.在处理Excel文件时,经常需要对单元格进行样式设置,以满足特定的需求和美化要求,通过使用Java中的相关库和API,我们可以轻松地操作Exce ...

  5. Programming Abstractions in C阅读笔记:p202-p234

    <Programming Abstractions in C>学习第65天,p202-p234总结. 一.技术总结 完成第五章学习,第五章介绍递归在实际问题中的进一步应用,例如汉诺塔问题, ...

  6. Programming abstractions in C阅读笔记:p184-p195

    <Programming Abstractions In C>学习第61天,p184-p195总结. 一.技术总结 1.mutual recursion 2.natural number ...

  7. 华为云MVP高浩:打破AI开发瓶颈,解决数据、算法、算力三大难题

    摘要:在高浩看来,大量的数字蓝领人才和AI应用开发人员构成了当前AI行业发展人才之基,这也为高校学生就业初期从事的工作指明了方向,而华为ModelArts平台在教育领域有着天然的数据.算法优势,非常适 ...

  8. 详解MRS CDL整体架构设计

    摘要:MRS CDL是FusionInsight MRS推出的一种数据实时同步服务,旨在将传统OLTP数据库中的事件信息捕捉并实时推送到大数据产品中去,本文档会详细为大家介绍CDL的整体架构以及关键技 ...

  9. Redis现网那些坑:用个缓存,还要为磁盘故障买单?

    摘要:向业务查询超时say goodbye! 本文分享自华为云社区<Redis现网那些坑:用个缓存,还要为磁盘故障买单?>,作者: GaussDB 数据库 . 近日,网上一些电商用户出现了 ...

  10. App加固中的代码混淆功能,让逆向工程师很头疼

    ​ App加固中的代码混淆功能,让逆向工程师很头疼 "我想离开浪浪山." 在数次尝试破解某个App 时,某个逆向工程师无奈感慨道. ​ 逆向工程师顾名思义就是把一个个完整的软件逆推 ...