Git is a powerful version control system that provides numerous commands and options to control and manipulate the version history of your code. Among those commands, git diff
and git log
are frequently used.
git diff
is used to show changes between commits, commit and working tree, etc., while git log
is used to show commit history.
To see differences between specific commits, you would need to use these two commands in conjunction. First, use git log
to identify the specific commits between which you want to see the differences. The basic command for viewing the git log is:
git log
This will give you a list of commits for that repository. Each commit will be represented with a commit hash, author, date, and commit message.
The commit hash is a long string of alphanumeric characters. This hash is a unique identifier for each commit. For the sake of simplicity, we will be needing the first 7 characters of this hash to identify a commit.
Let’s say that we have identified two commits:
commit1
represented by the hashabcd123
commit2
represented by the hashefgh456
To see the differences between these two commits, you can use git diff
as follows:
git diff abcd123 efgh456
This command will provide a detailed line-by-line breakdown of what has changed between the two commit states.
If you’re only interested in seeing which files changed (not the specific changes within those files), you can add the --stat
flag as follows:
git diff --stat abcd123 efgh456
The --stat
option will print out statistics regarding the diff, showing you how many insertions and deletions occurred, and in which files those changes happened.
Additionally, if you want to see a summary of the commit differences including new and deleted files, you can use the --summary
option:
git diff --summary abcd123 efgh456
Remember, you can also compare a specific commit to your current working directory by omitting one hash:
git diff abcd123
This will show the difference between the commit abcd123
and your current uncommitted work.
Lastly, if you want to check the difference between the last commit and the current state, you can use HEAD
keyword:
git diff HEAD
In summary, git diff
and git log
can be used in conjunction to explore the history of a codebase and the changes that have happened over time.