Comparing Branches and Examining Specific Commits
Now that we can view the commit history of remote branches, let's learn how to compare branches and examine specific commits in more detail.
Comparing Branches
Git provides powerful tools to compare different branches. Since all branches in our git-playground repository point to the same commit, let's first understand how to use these commands, then we'll see practical examples.
To see the differences between your current branch and a remote branch, use:
git diff origin/main
Since all branches are identical in our example, this will show no differences. However, in real projects, this would show what has changed between your current branch and the remote branch.
You can also compare two remote branches:
git diff origin/master origin/feature-branch
Again, since they're identical, no differences will be shown.
Examining Specific Commits
Sometimes you need to examine a specific commit in detail. You can do this using the git show command followed by the commit hash:
First, let's get the commit hashes:
git log --oneline origin/master
Now examine a specific commit using its hash:
git show d22f46b
This command shows the commit details along with the changes introduced in that commit. You'll see the addition of file2.txt.
Let's examine the commit that added file1.txt:
git show cf80005
And the initial commit:
git show b00b937
Viewing File Contents from a Remote Branch
To see the content of a specific file as it exists in a remote branch, use:
git show origin/master:README.md
This displays the content of README.md as it exists in the master branch on the remote repository.
You can also see the content of other files:
git show origin/master:file1.txt
git show origin/master:file2.txt
Checking Who Modified a Specific Line
To see who made changes to a particular file and when, use the git blame command:
git blame README.md
This displays each line of the file along with the commit hash, author, and date of the last change to that line.
Try it with the other files:
git blame file1.txt
git blame file2.txt
Viewing File History
To see how a specific file has evolved over time:
git log -p origin/master -- README.md
This shows all commits that modified the README.md file in the remote master branch, along with the changes made in each commit.
These commands provide valuable tools for understanding the history and evolution of your codebase, making it easier to track changes, debug issues, and collaborate with other developers.