git的历史记录
1.查看git日志
git log 查看全部commit信息
git log -p 查看全部commit信息和代码的差异
git log -1 查看近1条的commit信息
git log -10 查看近10条的commit信息
git log 7b1558c 根据提交名称的缩写查询commit信息(通常是4-5位,但是7-8位保证匹配的唯一性)
2.指定范围查找
git log --before="5 hours" -1 查看最近5小时的提交,--since和--before接收大多数英文格式的日期,例如24hours 1minute 2008-10.01等
git log 18f822e..0bb3dfb 指定最老版本和最新版本之间的提交 注意:该区间左开右闭
git log 18f822e..HEAD HEAD代表版本库中当前分支末稍的最新版本
git log 18f822e.. 相当与git log 18f822e..HEAD
git log --pretty=format:"%h %s" 1.0..HEAD
^脱字号,相当与回溯一个版本18f822e^表示18f822e之前的那个版本,18f822e^^表示18f822e之前的版本的之前的版本。
(Windows下,如果版本号带有脱字号,那么要加双引号,否则不识别,如18f822e^则不识别,应该为"18f822e^")
*~N 波浪字符加数字,指回溯n个版本,18f822e~1是指18f822e的父节点,18f822e~2是指18f822e的祖父节点
git log -1 HEAD^^^
git log -1 HEAD^~2
git log -1 HEAD~1^^
git log -1 HEAD~3
也可以用在查询范围中
git log HEAD~10..HEAD
3.查看版本差异
git diff 18f822e 显示版本18f822e与当前工作目录树间的差异
git diff --stat 1.0 得到变更统计数据
4.查明责任人
git blame hello.html 查看特定代码快的历史信息,返回的信息中前缀中包含提交名称,提交人,提交时间
结果:
^7b1558c index.html (Travis Swicegood 2008-09-21 14:20:21 -0500 1) <html>
a5dacabd index.html (Travis Swicegood 2008-09-21 20:37:47 -0500 2) <head>
git blame -L 12,13 hello.html 查看12行到13行的日志
git blame -L 12,+2 hello.html
结果:
^7b1558c index.html (Travis Swicegood 2008-09-21 14:20:21 -0500 12) </body>
^7b1558c index.html (Travis Swicegood 2008-09-21 14:20:21 -0500 13) </html>
git blame -L 12,-2 hello.html
结果:
4333289e index.html (Travis Swicegood 2008-09-22 07:54:28 -0500 11) </ul>
^7b1558c index.html (Travis Swicegood 2008-09-21 14:20:21 -0500 12) </body>
git blame -L "/<\/body>/",+2 hello.html
结果:
^7b1558c index.html (Travis Swicegood 2008-09-21 14:20:21 -0500 12) </body>
^7b1558c index.html (Travis Swicegood 2008-09-21 14:20:21 -0500 13) </html>
git blame -L "/<\/body>/",-2 4333289e^ -- hello.html 查看文件hello.html在4333289e之前的提交日志
结果: 出错
git blame -L "/<\/body>/",-2 4333289e^ -- index.html 查看文件index.html在4333289e之前的提交日志
结果:
^7b1558c (Travis Swicegood 2008-09-21 14:20:21 -0500 7) <h1>Hello World!</h1>
^7b1558c (Travis Swicegood 2008-09-21 14:20:21 -0500 8) </body>
5.跟踪内容
git blame -M original.txt -M表示告诉命令git blame检测在同一个文件内移动或者复制的代码行
git blame -C -C copy.txt -C -C 表示查看文件之间的复制
git log -C -C -l -p
6.撤销修改
<1>增补提交
git commit -C HEAD -a --amend 告诉git复用指定提交的提交留言,而不是从头再写一个git commit -c HEAD -a --amend 功能同上,不同点:打开编译器
<2>反转提交
撤销已经提交的git revert -n HEAD-n表示告诉git先不要提交,如果不加-n,那么git会立即提交git revert -n 540ecb7反转54ecb7git commit -m "revert 45eaf98 and 540ecb7"反转好该反转的然后再统一的提交
<3>复位
复位版本库到一个特定的版本,它是以提交名称作为参数的,默认是HEAD,^和~都可以使用git reset --soft HEAD--sof参数它使得git暂存所有的因复位带来的差异,但不提交他们。git reset --hard HEAD^--hard参数会从版本库和工作目录树中同时删除提交,并且不可恢复。
7.重新改写历史记录
<1>给历史记录重新排序
git rebase -i HEAD~3git log --pretty=format:"%h %s" HEAD~3..
<2>将多个提交压合成一个提交
git rebase -i 0bb3dfb^
<3>将一个提交分解成多个提交
git rebase -i HEAD~4git log -lgit reset HEAD^git diffgit commit -m "message1" -agit commit -m "message2" -agit rebase --continue