乐闻世界logo
搜索文章和话题

How can I see the changes in a Git commit?

1个答案

1

When you want to view changes in Git commits, you can use the following commands:

  1. git log This 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:

    sh
    git log --oneline

    If you want to view detailed changes for each commit, you can use:

    sh
    git log -p

    The -p parameter displays the specific differences (i.e., patches) for each commit.

  2. git show If you know the commit hash of a specific commit, you can use the git show command to view its detailed information, including the changes made.

    For example:

    sh
    git show commit_hash

    where commit_hash is the hash of the commit you want to inspect.

  3. git diff Although git diff is 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:

    sh
    git diff commit_hash1 commit_hash2

    where commit_hash1 and commit_hash2 are the hashes of the respective commits. If you specify only one commit, git diff compares 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:

sh
git 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.

2024年6月29日 12:07 回复

你的答案