Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Diffing Two Commits - A Pirate’s Guide to Git

Header Image

Ahoy mateys! Welcome aboard the Git ship. Today, we’ll be learning about a crucial command that every sailor should know: git diff. This command allows us to view the difference between two different commits in our Git repository. It’s a powerful tool that can help us understand how our code has changed over time and track down bugs and issues.

Viewing the Difference Between Two Different Commits

To use the git diff command, we’ll need to specify which commits we want to compare. Let’s say we want to compare the changes between the current commit and the commit before it. We can do that with the following command:

git diff HEAD^ HEAD

In this command, HEAD^ refers to the commit before the current one, and HEAD refers to the current commit. Running this command will display the changes between the two commits in our terminal.

But what if we want to compare two specific commits that aren’t the current one? We can do that too! We’ll just need to replace HEAD^ and HEAD with the commit hashes we want to compare. Here’s an example:

git diff 1234567 89abcdef

In this command, 1234567 and 89abcdef are the commit hashes we want to compare. Git will show us the differences between these two commits.

Code Examples

Here are some examples of how we can use the git diff command:

# Compare current commit to the previous one
git diff HEAD^ HEAD

# Compare two specific commits
git diff 1234567 89abcdef

Conclusion

And that’s all there is to it, me hearties! The git diff command is a useful tool that can help us understand how our code has changed over time. By comparing different commits, we can track down bugs, identify issues, and gain insight into the evolution of our code.

In this article, we’ve covered the basics of using git diff to view the difference between two different commits. We hope you found it helpful and informative. As always, happy sailing, and may your Git repositories always be bug-free!