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. To see the differences between your current branch and a remote branch, use:
git diff origin/feature-branch
This shows what has changed between your current branch (master
) and the remote feature-branch
.
You can also compare two remote branches:
git diff origin/master origin/feature-branch
This command shows what changes were made in the feature-branch
compared to master
.
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:
git log --oneline origin/feature-branch
First, get the commit hash from the log output, then examine that specific commit:
git show abcdef1
Replace abcdef1
with an actual commit hash from your log output. This command shows the commit details along with the changes introduced in that commit.
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/feature-branch:README.md
This displays the content of README.md
as it exists in the feature-branch
on the remote repository.
You can also see how a specific file has evolved over time:
git log -p origin/feature-branch -- README.md
This shows all commits that modified the README.md
file in the remote feature-branch
, along with the changes made in each commit.
Checking Who Modified a Specific Line
To see who made changes to a particular file and when, use the git blame
command:
git blame origin/feature-branch -- app.js
This displays each line of the file along with the commit hash, author, and date of the last change to that line.
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.