How to use the command git whatchanged (with examples)
Git is a powerful version control system that allows developers to track changes and collaborate on projects efficiently. The git whatchanged
command is used to display logs and changes for recent commits or files. It is a useful command for reviewing the history of a project and understanding what changes have been made. In this article, we will explore three use cases of the git whatchanged
command and provide examples for each.
Use case 1: Display logs and changes for recent commits
Code:
git whatchanged
Motivation:
By using the git whatchanged
command without any additional arguments, we can see an overview of the recent commits and their associated changes. This can be helpful when quickly reviewing the project’s history and understanding what changes have been made.
Explanation:
The command git whatchanged
without any arguments displays the logs and changes for recent commits. It shows the commit message, author, date, and summary of changes for each commit. This allows us to get a high-level overview of the project’s recent history.
Example output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7
Author: John Doe <john.doe@example.com>
Date: Thu Jan 1 12:00:00 2023 +0000
Added new feature XYZ
path/to/file1 | 10 ++++++++++
path/to/file2 | 20 +++++++++-----------
2 files changed, 18 insertions(+), 12 deletions(-)
commit b2a3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8
Author: Jane Smith <jane.smith@example.com>
Date: Wed Dec 31 12:00:00 2022 +0000
Fixed bug ABC
path/to/file3 | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
...
Use case 2: Display logs and changes for recent commits within the specified time frame
Code:
git whatchanged --since="2 hours ago"
Motivation:
Sometimes, we want to see the logs and changes for commits within a specific time frame. By using the --since
argument, we can limit the output to only show the commits and changes made within that time frame. This can be useful when reviewing recent changes for troubleshooting or debugging purposes.
Explanation:
The --since
argument allows us to specify a time frame to filter the output. In this case, we use the value "2 hours ago"
, which indicates that we want to see the logs and changes for commits made in the last two hours. This command will ignore any commits made before the specified time.
Example output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7
Author: John Doe <john.doe@example.com>
Date: Thu Jan 1 12:00:00 2023 +0000
Added new feature XYZ
path/to/file1 | 10 ++++++++++
path/to/file2 | 20 +++++++++-----------
2 files changed, 18 insertions(+), 12 deletions(-)
commit b2a3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8
Author: Jane Smith <jane.smith@example.com>
Date: Wed Dec 31 12:00:00 2022 +0000
Fixed bug ABC
path/to/file3 | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
...
Use case 3: Display logs and changes for recent commits for specific files or directories
Code:
git whatchanged path/to/file_or_directory
Motivation:
When working on a large project with many files and directories, it can be helpful to narrow down the logs and changes to a specific file or directory. By specifying a file or directory in the command, we can limit the output to only show the commits and changes related to that specific file or directory. This can be useful for comprehensively reviewing the changes made to a particular file or area of the project.
Explanation:
By providing the path to a specific file or directory as an argument to the git whatchanged
command, we instruct Git to only show the logs and changes associated with that file or directory. This allows us to focus on the relevant changes and understand the history of a particular file or area of the project.
Example output:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7
Author: John Doe <john.doe@example.com>
Date: Thu Jan 1 12:00:00 2023 +0000
Added new feature XYZ
path/to/file1 | 10 ++++++++++
path/to/file2 | 20 +++++++++-----------
2 files changed, 18 insertions(+), 12 deletions(-)
...
Conclusion:
The git whatchanged
command is a versatile tool for reviewing the history of a Git project and understanding the changes that have been made. Whether we want to see all recent commits, filter by a time frame, or focus on specific files or directories, git whatchanged
provides the flexibility to cater to our needs. By leveraging this command, we can gain valuable insights into the evolution of our project and facilitate collaboration among team members.