GIT使用—提交的查找与变更

一、查找提交

(1)git bisect(二分搜索法)
基于任意搜索条件查找特定的错误提交。在排查某个提交版本导致的错误时非常有用。

[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
此命令告诉你一个文件中的每一行最后是谁修改的和哪次提交做出了变更。

[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根据给定的条件沿着文件的差异历史搜索。通过搜索修订版本间的实际差异,这条命令可以找到那些执行改变的提交。

[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'参数来逆向显示所有日志。

[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 选项以隐藏合并提交

[root@localhost public_html]# git log --oneline --before={3.weeks.ago} --after={2018-01-01} --no-merges

三、更改提交

git reset命令会把版本库和工作目录改变为已知状态,其调整HEAD引用指向给定的提交,默认情况下还会更新搜索引以匹配该提交。

git reset-soft
将HEAD引用指向给定提交,索引和工作目录内容保持不变。

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>"
原文地址:https://www.cnblogs.com/tongxiaoda/p/8479107.html