Introduction
In this lab, you will learn how to effectively track the history of a file in Git, even after it has been renamed. We will explore the powerful git log --follow command to follow a file's history across name changes.
You will also learn how to use git diff --name-status to examine the status of files between commits, and finally, we will test these techniques on files that have not been renamed to understand the difference in output.
Run git log --follow -- File
In this step, we will explore how to track the history of a specific file, even if it has been renamed. This is where the git log --follow command comes in handy.
First, let's make sure we are in our project directory.
cd ~/project/my-time-machine
Now, let's create a new file and add some content.
echo "This is the original content." > original_file.txt
Add and commit this file.
git add original_file.txt
git commit -m "Add original file"
You should see output similar to this:
[master 1a2b3c4] Add original file
1 file changed, 1 insertion(+)
create mode 100644 original_file.txt
Now, let's rename the file.
git mv original_file.txt renamed_file.txt
Check the status to see the change.
git status
You should see something like this:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: original_file.txt -> renamed_file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
message.txt
Commit the rename.
git commit -m "Rename original file"
You should see output similar to this:
[master 5d6e7f8] Rename original file
1 file changed, 0 insertions(+), 0 deletions(-)
rename original_file.txt -> renamed_file.txt (100%)
Now, let's use git log to see the history of the renamed file.
git log renamed_file.txt
This will only show the commit where the file was renamed. To see the history before the rename, we need to use the --follow option.
git log --follow renamed_file.txt
This command will show you the history of the file, following its name changes. You should see both the "Rename original file" commit and the "Add original file" commit.
The git log --follow command is essential when you need to understand the complete history of a file that has been moved or renamed within your repository. It helps you trace the evolution of the file across different commits, regardless of its current name.
Use git diff --name-status
In this step, we will learn how to use git diff --name-status to see a summary of changes between commits, specifically focusing on the status and names of the files that have changed.
First, ensure you are in the correct directory:
cd ~/project/my-time-machine
Let's make another change to our renamed_file.txt.
echo "Adding a new line." >> renamed_file.txt
Now, let's see the status.
git status
You should see that renamed_file.txt has been modified.
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: renamed_file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
message.txt
no changes added to commit (use "git add" and/or "git commit -a")
Add the change to the staging area.
git add renamed_file.txt
Now, let's use git diff --name-status to see the changes between the last commit and the staged changes.
git diff --name-status --cached
The --cached option tells git diff to compare the staged changes (the index) with the last commit (HEAD).
You should see output similar to this:
M renamed_file.txt
The output M renamed_file.txt indicates that the file renamed_file.txt has been Modified.
Now, let's commit this change.
git commit -m "Add new line to renamed file"
You should see output similar to this:
[master 9h0i1j2] Add new line to renamed file
1 file changed, 1 insertion(+)
The git diff --name-status command is very useful for getting a quick overview of what kind of changes (added, modified, deleted, renamed, copied) have occurred between different points in your Git history, without showing the full content of the changes. This is particularly helpful when reviewing changes before committing or when comparing branches.
Test Non-Renamed Files
In this step, we will observe how git log --follow behaves with files that have not been renamed. This will help solidify your understanding of when --follow is necessary.
First, make sure you are in the correct directory:
cd ~/project/my-time-machine
We already have renamed_file.txt and message.txt in our repository. Let's add some content to message.txt.
echo "This is a message." >> message.txt
Check the status.
git status
You should see that message.txt has been modified and renamed_file.txt is up to date.
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: message.txt
no changes added to commit (use "git add" and/or "git commit -a")
Add and commit the change to message.txt.
git add message.txt
git commit -m "Add content to message file"
You should see output similar to this:
[master 3k4l5m6] Add content to message file
1 file changed, 1 insertion(+)
Now, let's use git log on message.txt.
git log message.txt
This will show you the history of message.txt. You should see the "Add content to message file" commit and the initial commit where message.txt was created (from the first lab).
Now, let's try git log --follow on message.txt.
git log --follow message.txt
You will notice that the output is the same as git log message.txt. This is because message.txt has not been renamed or moved. The --follow option is specifically designed to track file history across name changes. For files that haven't been renamed, git log is sufficient.
This step demonstrates that while git log --follow is powerful for tracking renamed files, for files that maintain their name, the standard git log command provides the complete history. Understanding this distinction helps you choose the right command for the task.
Summary
In this lab, we learned how to track the history of a file in Git, even after it has been renamed. We started by creating, adding, and committing a file, then renamed it using git mv and committed the rename. We then demonstrated that a simple git log on the new filename only shows the history from the rename forward.
The key takeaway was the use of the git log --follow <filename> command. This powerful option allows Git to trace the history of a file across name changes, providing a complete view of its evolution from creation to its current state. This is crucial for understanding the full context of a file's modifications within a project's history.



