【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
查看指定字符串的提交历史
# 若存在预期的修改,则输出至文件查找对应的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】用法小记的更多相关文章
- [转]HttpClient的超时用法小记
HttpClient的超时用法小记 HttpClient在使用中有两个超时时间,是一直接触和使用的,由于上次工作中使用httpClient造成了系统悲剧的情况,特地对它的两个超时时间进行了小小的测试, ...
- Matlab norm 用法小记
Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p) Returns sum(abs(A).^p)^(1/p), for ...
- linux expect, spawn用法小记
linux expect, spawn用法小记_IT民工_百度空间 linux expect, spawn用法小记 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://sys ...
- [转载]expect spawn、linux expect 用法小记
原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...
- Shell常见用法小记
shell的简单使用 最近发现shell脚本在平常工作中简直算一把瑞士军刀,很多场景下用shell脚本能实现常用的简单需求,而之前都没怎么学习过shell,就趁机把shell相关的语法和常见用法总结了 ...
- git用法之常用命令[克隆、提交]
1.克隆/下载项目 1)git clone git@git.soydai.cn:liuxuewen/static-file-3.0.git 或者 2)git clone http://git.soyd ...
- git用法
chapter: 8 add 添加文件内容至索引 用法:git add [选项] [--] ... -n, --dry-run 演习 -v, --verbose 冗长输出 -i, --interact ...
- Bitbucket - 用git 用法
核心流程: 从远端中心repo那里Git clone 到本地,再在本地开发(add, commit), 通常会利用branch管理,如果觉得code 没问题了,就push到远端的中心repo上.这里中 ...
- git用法大全
转载自实验楼,之前有更新过两篇git的文章,毕竟内容太少,而git还有很多更丰富的技能,在实验楼上有一系列全的教程,这里做一下备案.需要时查阅. Git 实战教程 目录 一.实验说明 二.git的初始 ...
- git用法汇总
使用了一年多的git命令了,昨晚竟然又出现了问题.虽然解决了,不过还是被罚了... 总结下自己常用的git命令和遇到的一些坑. 1)常用的命令 1. 从git远程分支clone代码: git clon ...
随机推荐
- 华企盾DSC可能涉及的Linux命令
掌握Linux系统的基本命令 详细使用请参考:Linux命令大全 uname 查看系统信息 wget 文件下载 chmod 改变文件或目录权限 ls 显示指定工作目录下的内容及属性信息 cd 切换当前 ...
- C++ Qt开发:QSqlDatabase数据库组件
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍QSqlDat ...
- 红日靶场4-wp
红日靶场4 环境搭建 注:130网段为模拟外网网段,111网段为内网网段 机器 用户 密码 网卡 kali root / 192.168.130.19 web(ubuntu) ubuntu ubunt ...
- Python——第二章:range()的用法:
在Python中,range()是一个内置函数,用于生成一个整数序列,通常用于循环遍历.以下是range()函数的一些常见用法: range(n): 从0数到n. 不包含nrange(m, n): 从 ...
- Navicat Premium多用户破解方法 12以上版本
https://www.cnblogs.com/cgqplus/p/15267306.html 本文不提供注册机或者破解工具,本论坛多的是,搜索一下就好了. 本方法适用于多用户环境下使用,比如在服务器 ...
- 关于helloworld
我们的helloworld是从一个源程序开始的,该源程序由程序员通过编译器创建并保存的文件,文件名就是hello.c.这个hello.c的源程序,实际上是有0和1组成的序列.每一个0和1都成为一位,这 ...
- 为什么Java中“1000==1000”为false,而”100==100“为true?
在日常编程中,我们经常遇到一些看似简单却隐藏着复杂逻辑的问题. 比如,你是否想过为什么在 Java 中表达式1000==1000会返回 false,而 100==100 却返回 true 呢? Int ...
- CF1340F Nastya and CBS 题解
题目大意 给定一个长度为 \(n(n \le 10 ^ 5)\) 的括号序列.要求支持两个操作: 修改某个位置的括号. 询问 \([l, r]\) 区间内的括号序列是否合法. 题目分析 显然,这道题是 ...
- 在winform blazor hybrid中绘图
前几天跟大家介绍了在winform中使用blazor hybrid,而且还说配上blazor的ui可以让我们的winform程序设计的更加好看,接下来我想以一个在winform blazor hybr ...
- 2023-05-13:你现在手里有一份大小为 n x n 的 网格 grid, 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋,1 代表陆地。 请你找出一个海洋单元格,这个海洋单元格
2023-05-13:你现在手里有一份大小为 n x n 的 网格 grid, 上面的每个 单元格 都用 0 和 1 标记好了其中 0 代表海洋,1 代表陆地. 请你找出一个海洋单元格,这个海洋单元格 ...