Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pipe output from git log in Git Bash?

Tags:

git

bash

Reason: I want to compare two arbitrary different commits using a difftool. I know the hashes from a search and I don't want to copy these hashes, thus I am looking for a command that does something like

$ log_str=$(git log --all -S"new_tour <-" --pretty=format:"%h")
$ git difftool -t kdiff3 log_str[1] log_str[2] myfile.txt
  • I would like to be able to address arbitrary indices - not always 1 and 2
  • It would be great if the answer also gives a hint, how to figure out, what the structure of log_str is. Is it a character? An array of characters? A list? ... using the Bash.

I found some related help here and here, but I can't make it work.
Now I do:

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

and

$ git difftool -t kdiff3 3f69dc7 b8242c6 myfile.txt
like image 782
Christoph Avatar asked Nov 29 '25 14:11

Christoph


1 Answers

I would take a two step approach using a temporary file:

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 is used to display line 1 and line 2 of the file.


With variables:

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

in a bash function:

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
like image 172
hek2mgl Avatar answered Dec 02 '25 05:12

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!