(1)git bisect(二分搜索法)
基于任意搜索条件查找特定的错误提交。在排查某个提交版本致使的错误时很是有用。html
[root@localhost public_html]# git bisect start 开始搜索 [root@localhost public_html]# git bisect bad 告知GIT那个提交时“坏”的,一般是当前版本 [root@localhost public_html]# git bisect good v2.6.27 告知那个版本提交是“好”的,一般是测试经过版本 以后Git会在范围内将每一个修订版本搜索出来,以后你要作的就是缩小排查范围。 最后要告诉Git已经完成 [root@localhost public_html]# git git bisect reset [root@localhost public_html]# git branch * master
(2)git blame
此命令告诉你一个文件中的每一行最后是谁修改的和哪次提交作出了变动。git
[root@localhost public_html]# git blame -L 1, index.html 0a307160 (tong 2018-02-27 11:52:29 +0800 1) <html> 0a307160 (tong 2018-02-27 11:52:29 +0800 2) <body> ^b4e2a14 (tong 2018-02-27 11:45:23 +0800 3) My website is alive! 0a307160 (tong 2018-02-27 11:52:29 +0800 4) </body> 0a307160 (tong 2018-02-27 11:52:29 +0800 5) </html>
(3)Pickaxe
git log -Sstring根据给定的条件沿着文件的差别历史搜索。经过搜索修订版本间的实际差别,这条命令能够找到那些执行改变的提交。web
[root@localhost public_html]# git log -Shtml --pretty=oneline --abbrev-commit index.html 0a30716 This is new html!
[root@localhost public_html]# git log commit aa431d938e85445f6c22c7389a37349f587d5b01 Author: tong <tongxiaoda@anzhi.com> Date: Tue Feb 27 13:06:21 2018 +0800 mv bar to foo commit 1ed9a862e62bd5513021838cb435cf8170e2173d Author: tong <tongxiaoda@anzhi.com> Date: Tue Feb 27 13:04:37 2018 +0800 new bar commit 19a473c2e935efa59b8edea19d2d12be96987a97 Author: tong <tongxiaoda@anzhi.com> Date: Tue Feb 27 12:00:03 2018 +0800 Remove a poem commit 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 Author: tong <tongxiaoda@anzhi.com> Date: Tue Feb 27 11:59:16 2018 +0800 add poem.html commit 0a3071601cc10777e271a952ead46cffba233e24 Author: tong <tongxiaoda@anzhi.com> Date: Tue Feb 27 11:52:29 2018 +0800 This is new html! commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d Author: tong <tongxiaoda@anzhi.com> Date: Tue Feb 27 11:45:23 2018 +0800 Initial contents of public_html
能够用 --graph 选项,查看历史中何时出现了分支、合并。测试
[root@localhost public_html]# git log --oneline --graph * aa431d9 mv bar to foo * 1ed9a86 new bar * 19a473c Remove a poem * 5be473e add poem.html * 0a30716 This is new html! * b4e2a14 Initial contents of public_html
能够用 '--reverse'参数来逆向显示全部日志。3d
[root@localhost public_html]# git log --reverse --oneline b4e2a14 Initial contents of public_html 0a30716 This is new html! 5be473e add poem.html 19a473c Remove a poem 1ed9a86 new bar aa431d9 mv bar to foo
若是只想查找指定用户的提交日志能够使用命令:git log --author日志
[root@localhost public_html]# git log --author=tong --oneline -3 aa431d9 mv bar to foo 1ed9a86 new bar 19a473c Remove a poem
若是你要指定日期,能够执行几个选项:--since 和 --before,可是你也能够用 --until 和 --after。
--no-merges 选项以隐藏合并提交code
[root@localhost public_html]# git log --oneline --before={3.weeks.ago} --after={2018-01-01} --no-merges
git reset命令会把版本库和工做目录改变为已知状态,其调整HEAD引用指向给定的提交,默认状况下还会更新搜索引以匹配该提交。htm
git reset-soft
将HEAD引用指向给定提交,索引和工做目录内容保持不变。blog
git reset--mixed(默认模式)
将HEAD指向给定提交,索引内容也跟着改变以符合给定提交的树结构,但工做目录中的内容保持不变。索引
git reset-hard
将HEAD指向给定提交,索引内容也跟着改变以符合给定提交的树结构,工做目录内容也随之改变以反映给定提交表示的树的状态。
简单地重作或清除分支上的最近提交
[root@localhost ~]# cd test [root@localhost test]# git init Initialized empty Git repository in /root/test/.git/ [root@localhost test]# echo foo >> master_file [root@localhost test]# git add master_file [root@localhost test]# git commit [master (root-commit) 96a97fb] This is test! 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 master_file [root@localhost test]# echo "more foo" >> master_file [root@localhost test]# git commit master_file [master 8b6ce36] Change master_file 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost test]# git show-branch --more=5 [master] Change master_file [master^] This is test! 假设第二次提交是错的,须要改变一下 [root@localhost test]# git reset HEAD^ Unstaged changes after reset: M master_file [root@localhost test]# git show-branch --more=5 [master] This is test! [root@localhost test]# cat master_file foo more foo 执行后Git离开了master_file的新状态,由于重置了索引,因此必须从新暂存你想要提交的修改 [root@localhost test]# echo "even more foo" >> master_file [root@localhost test]# git commit master_file [master eaf3fca] new change master_file 1 files changed, 2 insertions(+), 0 deletions(-) [root@localhost test]# git show-branch --more=5 [master] new change master_file [master^] This is test! 查看版本库中引用变化的历史记录 [root@localhost test]# git reflog eaf3fca HEAD@{0}: commit: new change master_file 96a97fb HEAD@{1}: HEAD^: updating HEAD 8b6ce36 HEAD@{2}: commit: Change master_file
使用git cherry-pick提交命令会在当前分支上应用给定提交引入的变动。这并不改变版本库中的现有历史记录,而是添加历史记录。
一般用于版本库中一个分支的特定提交引入不一样的分支中,即把维护分支的提交移植到开发分支。
git checkout rel_2.3 git cherry-pick dev~2
使用git revert将引入一个新提交来抵消给定提交的影响。
git revert master~3
修改最新提交
git commit --amend --author "Bob <Bob@test.com>"