Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare different files from two separate commits

Tags:

git

Let's suppose a friend changed the contents of a file and moved the file like mv old_name new_name and created a single commit for both of those changes.

With a preference for using git, is there a way to compare the contents of old_name at commit A with the contents of new_name at commit B? I can think of a work around, which is to checkout to commit B, create a new branch, rename new_name to old_name in commit C and then diff old_name between A and C, but I would like something a little more straightforward.

like image 286
philosofool Avatar asked Feb 03 '26 15:02

philosofool


1 Answers

git can compare blobs or trees at will.

Try this

git diff some-commit:file-in-this-commit other-commit:file-in-the-other-commit

That should work. It can even compare stuff in the same commit.... Or in HEAD.... It comes handy

git diff HEAD:one-file HEAD:another-file

I use it when I want to compare things that are "related" to see how they diff. It might look like "good'ol" diff could do the same thing.... And sure, it works.... But only for things in HEAD.... If you wanted to compare stuff in other commits, git diff is what you need.

like image 115
eftshift0 Avatar answered Feb 06 '26 07:02

eftshift0



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!