Today, I learned how to use
git diff to compare changes in a Git repository against the last commit.
This can be incredibly useful for reviewing the modifications made since the previous commit, ensuring that only the intended changes are included before committing again.
In this post, I'll walk you through the simple
steps step to perform this action and understand its output.
Talk is cheap, show me the code!
git diff $(git log --pretty=format:'%H' -n 2 | xargs)
And now you may think: "Just that?" Yes, just that. But it's a very pow.
Let's break it down and understand what's going on here:
git log --pretty=format:'%H' -n 2: This part of the command retrieves the commit hashes of the last two commits. The
--pretty=format:'%H'option formats the output to show only the full commit hashes, and the
-n 2option limits the output to the last two commits.
| xargs: The pipe (
|) is used to redirect the output of the previous command (the commit hashes) as input to the
xargsis a Unix command that reads items from standard input and executes a command with those items as arguments.
git diff: This is the main command that compares two commit hashes provided by the
xargspasses the two commit hashes as arguments to
git diff, the command effectively becomes
git diff <commit_hash_1> <commit_hash_2>, which compares the changes between the two specified commits.
So, in summary, this command shows the differences between the last two commits in a Git repository by passing their commit hashes to the
git diff command.
See you in the next one!