Continuing from my last post, which explained my Git workflow, in this post I’m sharing how to compare different versions of files. Before that, you might want to read the Getting started with Git post too if you haven’t done already.
The main objective of version controlling is to enable you to work with different versions of files. Git provides a command diff to let you to compare different versions of your files.
The most common scenario to use diff is to see what changes you made after your last commit. Let’s see how to do it.
I opened the helloworld project from my last example with a clean working directory. i.e. I have already committed all my code changes. So, a git status will give an output like this:
Let’s make a change in the helloworld.txt file now and compare this file with previously committed version.
There it is. Git shows the exact change I made in the file. But, if you look at the diff command, you might wonder what HEAD is doing there! Well, it is there for a purpose.
If you can recall, Git has an index between local repository and your working directory. So most of Git commands can either refer to index or the local repo. When you say HEAD in your Git command, it refers the local repo.
You can also compare files between two different commits. Every commit in Git has a commit id which you can get when you give git log. Then you can use the commit id if diff command like this.
You can compare not just a single file, but all your changes at once. If you made changes in many files, just don’t mention any file name in the diff command which will diff all the changed files.
That’s it about the basic introduction to compare files in Git. If you have any comments about this series, do let me know. I’ll continue with other topics in my next post.