Fetch and Verify git log
In this step, we will learn how to fetch information about commits from a remote repository and then view the commit history using git log
. Unlike git ls-remote
, which only shows references, git fetch
downloads the actual commit objects.
First, let's create a local Git repository to work with. Navigate to your project directory and initialize a new Git repository:
cd ~/project
mkdir my-remote-test
cd my-remote-test
git init
Now, we will add a remote repository. We'll use the same Git project repository on GitHub as in the previous step.
git remote add origin https://github.com/git/git.git
This command adds a remote named origin
with the specified URL. origin
is a conventional name for the primary remote repository.
Next, we will fetch the commit information from the remote repository:
git fetch origin
This command downloads all the branches and tags from the origin
remote, but it doesn't merge them into your current local branches. It updates your remote-tracking branches (e.g., origin/master
, origin/main
).
Now that we have fetched the commit information, we can view the commit history of a remote branch using git log
. Let's view the log for the master
branch on the origin
remote:
git log origin/master
You will see a long list of commits from the Git project's master branch. This shows the commit history that you just fetched from the remote.
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (origin/master, origin/HEAD)
Author: ...
Date: ...
...
commit ...
Author: ...
Date: ...
...
... (many more commits)
Press q
to exit the log view.
This process of fetching and then viewing the log allows you to inspect the history of a remote repository without affecting your local working directory or branches. It's a safe way to see what changes have been made on the remote.