gpt4 book ai didi

bash - 如何在 Git Bash 中通过管道传输 git log 的输出?

转载 作者:行者123 更新时间:2023-12-05 05:16:51 26 4
gpt4 key购买 nike

原因:我想使用 difftool 比较两个任意不同的提交。我从搜索中知道哈希值,但我不想复制这些哈希值,因此我正在寻找一个执行类似操作的命令

$ log_str=$(git log --all -S"new_tour <-" --pretty=format:"%h")
$ git difftool -t kdiff3 log_str[1] log_str[2] myfile.txt
  • 我希望能够处理任意索引——不总是 1 和 2
  • 如果答案也给出提示,如何弄清楚 log_str 的结构是什么,那就太好了。是一个角色吗?字符数组?一个列表? ... 使用 Bash。

我找到了一些相关的帮助herehere ,但我无法让它工作。
现在我这样做:

$ git log --pretty=format:"%h"
3f69dc7
b8242c6
01aa74f
903c5aa
069cfc5

$ git difftool -t kdiff3 3f69dc7 b8242c6 myfile.txt

最佳答案

我会使用临时文件采取两步法:

git log --all -S'SEARCH' --pretty=format:"%h" > tmp_out
git diff "$(sed -n '1p' tmp_out)" "$(sed -n '2p' tmp_out)" myfile.txt
rm tmp_out

sed用于显示文件的第一行和第二行。


有变量:

search="foo"
index_a="1"
index_b="2"
file="myfile.txt"
git log --all -S"${search}" --pretty=format:"%h" > tmp_out
git diff "$(sed -n "${index_a}p" tmp_out)" "$(sed -n "${index_b}p" tmp_out)" "${file}"
rm tmp_out

在 bash 函数中:

search_diff() {
search="${1}"
index_a="${2}"
index_b="${3}"
file="${4}"
git log --all -S"${search}" --pretty=format:"%h" > tmp_out
git diff "$(sed -n "${index_a}p" tmp_out)" "$(sed -n "${index_b}p" tmp_out)" "${file}"
rm tmp_out
}

search_diff "foo" 2 3 myfile.txt

关于bash - 如何在 Git Bash 中通过管道传输 git log 的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com