【Git】用法小记

解决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

查询提交信息中关键字

git log --grep="string"
# 区分大小写
git log -i --grep="string"
# 多个字符串
git log --grep="string1\|string2"

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

# 差异上下文匹配字符串
git log -p |grep "string"
# 指定文件查找
git log -p *.c |grep "string"
# 若存在预期的修改,可输出至文件查找对应的commit-id
git log -p >log_file.txt
# 查找字符串基于文件视角输出提交信息
git log -p -G "$arg3" --name-only
# 利用变化前后**增减**的字符串查找预期的提交
git log -p -S "string"

查看指定文件或路径的提交历史

git log -- <file_path>
# 查看指定提交的指定文件的修改
git show <commit_hash>:<file_path>

查看差异时排除指定目录

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]

posted @ 2021-12-01 11:14  壹点灵异  阅读(121)  评论(0编辑  收藏  举报