I'm creating a shell script that displays the commit IDs of the 5 last git commits of my git repository.
Name of the script file: 'git_commit.sh' By "commit ID", I mean the full 40-character hexadecimal string. By git repository, I mean the current branch.
The output I expect:
yossi@hostname$ bash git_commit.sh | cat -e
42dfd4bdba4646b28136c4b0d71f76ae352aac10$
63a7f8e9e7f5ab2f29c4227440d78fc731201b89$
57361b98c8cb9041759461093ed38b7b8930e12e$
a05251da02f6c677f38f3ab68c95b2dd8812a822$
d17402aa3e3835e1b9e444482cda233e97e032d6$
yossi@hostname$
The command that was inserted in 'git_commit.sh':
git log -5 --pretty="format:%H"
And here's the output I get when I run the script:
yossi@hostname$ bash git_commit.sh | cat -e
42dfd4bdba4646b28136c4b0d71f76ae352aac10$
63a7f8e9e7f5ab2f29c4227440d78fc731201b89$
57361b98c8cb9041759461093ed38b7b8930e12e$
a05251da02f6c677f38f3ab68c95b2dd8812a822$
d17402aa3e3835e1b9e444482cda233e97e032d6yossi@hostname$
PROBLEM
- The 5th and last commit ID is not followed by a trailing $.
- There's no line break before the next command prompt.
Which means that there was no newline after the 5th and last commit ID when it was piped into the 'cat -e' command.
I thoroughly reviewed the manual page of 'git log' and wasn't able to find anything that explains that behavior.
ALTERNATIVES THAT I TESTED
I tested the replacement of git log -5 --pretty="format:%H" by the following three commands:
git log -5 --format="%H" | cat -egit log -5 --pretty="tformat:%H" | cat -egit log -5 --pretty="%H" | cat -e
Each of those three alternative commands inserted in 'git_commit.sh' provide the expected output:
yossi@hostname$ bash git_commit.sh | cat -e
42dfd4bdba4646b28136c4b0d71f76ae352aac10$
63a7f8e9e7f5ab2f29c4227440d78fc731201b89$
57361b98c8cb9041759461093ed38b7b8930e12e$
a05251da02f6c677f38f3ab68c95b2dd8812a822$
d17402aa3e3835e1b9e444482cda233e97e032d6$
yossi@hostname$
Is someone able to explain the difference in behavior of git log -5 --pretty="format:%H"?
The explanation lies here:
Note that both
git log --format="%H"andgit log --pretty="%H"are equivalent to--pretty="tformat:%H":