When you want to view changes in Git commits, you can use the following commands:
-
git logThis command displays the commit history of the entire repository. You can view specific commit details by adding parameters.For example, the following command shows a concise summary of all commits in one line:
shgit log --onelineIf you want to view detailed changes for each commit, you can use:
shgit log -pThe
-pparameter displays the specific differences (i.e., patches) for each commit. -
git showIf you know the commit hash of a specific commit, you can use thegit showcommand to view its detailed information, including the changes made.For example:
shgit show commit_hashwhere
commit_hashis the hash of the commit you want to inspect. -
git diffAlthoughgit diffis primarily used to compare differences between the working directory and staging area, it can also be used to view differences between two commits.For example, the following command compares the differences between two different commits:
shgit diff commit_hash1 commit_hash2where
commit_hash1andcommit_hash2are the hashes of the respective commits. If you specify only one commit,git diffcompares that commit with the current working directory.
These commands are the fundamental tools for viewing changes in Git. You can combine them with various parameters as needed to retrieve different information. For example, to view the commit history of a specific file, you can use:
shgit log -p -- [file_path]
Additionally, if you are using graphical interface tools like GitKraken or SourceTree, these tools typically provide a more intuitive way to browse and view changes in historical commits.
For instance, in a project where I am responsible for code review, I frequently check changes in commits. I typically use git log -p to view detailed changes for each commit, allowing me to see modifications to every line of code. When I want to quickly locate an issue, I might use git blame [file_path] to identify which commit introduced the most recent changes to each line of code, helping to diagnose the problem.