本文介紹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 <[email protected]>
Date: Tue Feb 27 13:06:21 2018 +0800
mv bar to foo
commit 1ed9a862e62bd5513021838cb435cf8170e2173d
Author: tong <[email protected]>
Date: Tue Feb 27 13:04:37 2018 +0800
new bar
commit 19a473c2e935efa59b8edea19d2d12be96987a97
Author: tong <[email protected]>
Date: Tue Feb 27 12:00:03 2018 +0800
Remove a poem
commit 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31
Author: tong <[email protected]>
Date: Tue Feb 27 11:59:16 2018 +0800
add poem.html
commit 0a3071601cc10777e271a952ead46cffba233e24
Author: tong <[email protected]>
Date: Tue Feb 27 11:52:29 2018 +0800
This is new html!
commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
Author: tong <[email protected]>
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指向給定送出,索引内容也跟着改變以符合給定送出的樹結構,工作目錄内容也随之改變以反映給定送出表示的樹的狀态。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuMDNygjNyIzNz0CN1ITMzADM1EzNyIDM4EDMy0CM5YTOxITMvwlMwgTMwIzLcBTO2kTMyEzLcd2bsJ2Lc12bj5ycn9Gbi52YugTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
簡單地重做或清除分支上的最近送出
[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 <[email protected]>"