The “git diff” command in Git is used to show the differences or changes between the current state of your repository and the previous commit, or between different branches, commits, or revisions. It primarily focuses on tracking changes made to the files that are already being tracked by Git.
When you run the “git diff” command without any additional arguments, it compares the changes between your working directory (the files you have made modifications to) and the staging area (the files that have been added using “git add”). It shows the differences in a unified diff format, indicating the lines that have been added, modified, or deleted.
You can also use “git diff” with various options and arguments to compare different versions of files or different branches. For example, you can compare the changes between two commits, two branches, or a specific file across different commits.
The “git diff” command is a versatile tool that helps you visualize the differences in your codebase, making it easier to review changes, identify issues, and understand the impact of modifications. It provides valuable insights into the modifications made to your files, allowing you to track and manage the progress of your development work.
By using “git diff,” you can effectively analyze the changes in your code, collaborate with others, and make informed decisions before committing your changes. It is an essential command in Git for understanding the evolution of your repository and maintaining the integrity of your codebase.
git diff Command Examples
1. Show unstaged, uncommitted changes:
# git diff
2. Show all uncommitted changes (including staged ones):
# git diff HEAD
3. Show only staged (added, but not yet committed) changes:
# git diff --staged
4. Show changes from all commits since a given date/time (a date expression, e.g. “1 week 2 days” or an ISO date):
# git diff 'HEAD@[3 months|weeks|days|hours|seconds ago]'
5. Show only names of changed files since a given commit:
# git diff --name-only commit
6. Output a summary of file creations, renames and mode changes since a given commit:
# git diff --summary commit
7. Compare a single file between two branches or commits:
# git diff branch_1..branch_2 [--] /path/to/file
8. Compare different files from the current branch to other branch:
# git diff branch:/path/to/file2 /path/to/file